#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

int Cutsecond(int Div[],int i,int n,int k1,int k2)
{
    int s=0;
    int temp=0;
    while(i<n)
    {
        temp+=Div[i];
        if(temp==k1)
        {
            if(i!=n-1)
            s++;
        }
        if(temp==k2 && k1!=k2)
        {
           if(i!=n-1)
            s++;
        }
        if(temp>k2)break;
        i++;
    }
    return s;
}
int Cutfirst(int Div[],int n,int ton[])
{
    if(n<3)return 0;
    int s=0;
    int temp=0;
    for(int i=0;i<n;i++)
    {
        temp+=Div[i];
        if(temp==ton[0])
        {s+=Cutsecond(Div,i+1,n,ton[1],ton[2]);}
        if(temp==ton[1] && ton[0]!=ton[1])
        {s+=Cutsecond(Div,i+1,n,ton[0],ton[2]);}
        if(temp==ton[2] && ton[2]!=ton[1] && ton[2]!=ton[0])
        {s+=Cutsecond(Div,i+1,n,ton[0],ton[1]);}
        if(temp>ton[2])break;
    }
    return s;
}
int Cut(int Row[],int n,int Clo[],int m,int ton[])
{
    int s;
    s=Cutfirst(Row,n,ton)+Cutfirst(Clo,m,ton);
    return s;
}
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n,m;
    cin>>n>>m;
    int k;
    int Row[50],Clo[50];
    memset(Row,0,sizeof(Row));
    memset(Clo,0,sizeof(Clo));
    int i,j;
    for(i=0;i<n;i++)
    for(j=0;j<m;j++)
    {
        cin>>k;
        Row[i]+=k;
        Clo[j]+=k;
    }

    int ton[3];
    cin>>ton[0]>>ton[1]>>ton[2];
    for(i=0;i<3;i++)
    for(j=i+1;j<3;j++)
    {
        if(ton[i]>ton[j])
        {
            k=ton[i];
            ton[i]=ton[j];
            ton[j]=k;
        }
    }
    int s;

    s=Cut(Row,n,Clo,m,ton);
    cout<<s<<endl;
    return 0;
}


#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

int Cutsecond(int Div[],int i,int n,int k1,int k2)
{
    int s=0;
    int temp=0;
    while(i<n)
    {
        temp+=Div[i];
        if(temp==k1)
        {
            if(i!=n-1)
            s++;
        }
        if(temp==k2 && k1!=k2)
        {
           if(i!=n-1)
            s++;
        }
        if(temp>k2)break;
        i++;
    }
    return s;
}
int Cutfirst(int Div[],int n,int ton[])
{
    if(n<3)return 0;
    int s=0;
    int temp=0;
    for(int i=0;i<n;i++)
    {
        temp+=Div[i];
        if(temp==ton[0])
        {s+=Cutsecond(Div,i+1,n,ton[1],ton[2]);}
        if(temp==ton[1] && ton[0]!=ton[1])
        {s+=Cutsecond(Div,i+1,n,ton[0],ton[2]);}
        if(temp==ton[2] && ton[2]!=ton[1] && ton[2]!=ton[0])
        {s+=Cutsecond(Div,i+1,n,ton[0],ton[1]);}
        if(temp>ton[2])break;
    }
    return s;
}
int Cut(int Row[],int n,int Clo[],int m,int ton[])
{
    int s;
    s=Cutfirst(Row,n,ton)+Cutfirst(Clo,m,ton);
    return s;
}
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n,m;
    cin>>n>>m;
    int k;
    int Row[50],Clo[50];
    memset(Row,0,sizeof(Row));
    memset(Clo,0,sizeof(Clo));
    int i,j,sum=0;
    for(i=0;i<n;i++)
    for(j=0;j<m;j++)
    {
        cin>>k;
        Row[i]+=k;
        Clo[j]+=k;
        sum+=k;
    }

    int ton[3];
    cin>>ton[0]>>ton[1]>>ton[2];
    if(sum!=(ton[0]+ton[1]+ton[2])){
        cout<<"0"<<endl;
        return 0;
    }
    for(i=0;i<3;i++)
    for(j=i+1;j<3;j++)
    {
        if(ton[i]>ton[j])
        {
            k=ton[i];
            ton[i]=ton[j];
            ton[j]=k;
        }
    }
    int s;

    s=Cut(Row,n,Clo,m,ton);
    cout<<s<<endl;
    return 0;
}


#include <iostream>
#include<fstream>
using namespace std;

int Cut(int Div[],int n,int ton[])
{
    if(n<3)return 0;
    int s=0,temp=0,temp1;
    for(int i=0;i<n;i++)
    {
        temp+=Div[i];
        if(temp==ton[0])
        {
            temp1=0;
            for(int j=i+1;j<n;j++)
            {
                temp1+=Div[j];
                if(temp1==ton[1])
                {
                    s++;
                }
                if(temp1==ton[2] && ton[1]!=ton[2])
                {s++;break;}
                if(temp1>=ton[2])break;
            }
        }
        if(temp==ton[1] && ton[0]!=ton[1])
        {
            temp1=0;
            for(int j=i+1;j<n;j++)
            {
                temp1+=Div[j];
                if(temp1==ton[0])s++;
                if(temp1==ton[2]){s++;break;}
                if(temp1>=ton[2])break;
            }
        }
        if(temp==ton[2] && ton[2]!=ton[1])
        {
            temp1=0;
            for(int j=i+1;j<n;j++)
            {
                temp1+=Div[j];
                if(temp1==ton[0])s++;
                if(temp1==ton[1]&& ton[1]!=ton[0]){s++;break;}
                if(temp1>=ton[1])break;
            }
        }
        if(temp>=ton[2])break;
    }
    return s;
}
int main()
{
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
    int n,m,i,j,k;
    int*Row;
    int*Clo;
    cin>>n>>m;
    Row=new int[n];
    Clo=new int[m];
    for(i=0;i<n;i++)
        Row[i]=0;
    for(i=0;i<m;i++)
        Clo[i]=0;
    for(i=0;i<n;i++)
    {
    for(j=0;j<m;j++)
    {
        cin>>k;
        Row[i]+=k;
        Clo[j]+=k;
    }
    }
    int ton[3];
    cin>>ton[0]>>ton[1]>>ton[2];
    for(i=0;i<3;i++)
    {
        for(j=i+1;j<3;j++)
        {
            if(ton[i]>ton[j])
            {
                k=ton[i];
                ton[i]=ton[j];
                ton[j]=k;
            }
        }
    }
    int s=Cut(Row,n,ton)+Cut(Clo,m,ton);
    cout<<s<<endl;
    return 0;
}


#include <iostream>
#include<fstream>
using namespace std;

int Cut(int Div[],int n,int ton[])
{
    if(n<3)return 0;
    int s=0,temp=0,temp1;
    for(int i=0;i<n;i++)
    {
        temp+=Div[i];
        if(temp==ton[0])
        {
            temp1=0;
            for(int j=i+1;j<n;j++)
            {
                temp1+=Div[j];
                if(temp1==ton[1])
                {
                    s++;
                }
                if(temp1==ton[2] && ton[1]!=ton[2])
                {s++;break;}
                if(temp1>=ton[2])break;
            }
        }
        if(temp==ton[1] && ton[0]!=ton[1])
        {
            temp1=0;
            for(int j=i+1;j<n;j++)
            {
                temp1+=Div[j];
                if(temp1==ton[0])s++;
                if(temp1==ton[2]){s++;break;}
                if(temp1>=ton[2])break;
            }
        }
        if(temp==ton[2] && ton[2]!=ton[1])
        {
            temp1=0;
            for(int j=i+1;j<n;j++)
            {
                temp1+=Div[j];
                if(temp1==ton[0])s++;
                if(temp1==ton[1]&& ton[1]!=ton[0]){s++;break;}
                if(temp1>=ton[1])break;
            }
        }
        if(temp>=ton[2])break;
    }
    return s;
}
int main()
{
 //   freopen("input.txt","r",stdin);
 //   freopen("output.txt","w",stdout);
    int n,m,i,j,k;
    int*Row;
    int*Clo;
    cin>>n>>m;
    Row=new int[n];
    Clo=new int[m];
    for(i=0;i<n;i++)
        Row[i]=0;
    for(i=0;i<m;i++)
        Clo[i]=0;
    for(i=0;i<n;i++)
    {
    for(j=0;j<m;j++)
    {
        cin>>k;
        Row[i]+=k;
        Clo[j]+=k;
    }
    }
    int ton[3];
    cin>>ton[0]>>ton[1]>>ton[2];
    for(i=0;i<3;i++)
    {
        for(j=i+1;j<3;j++)
        {
            if(ton[i]>ton[j])
            {
                k=ton[i];
                ton[i]=ton[j];
                ton[j]=k;
            }
        }
    }
    int s=Cut(Row,n,ton)+Cut(Clo,m,ton);
    cout<<s<<endl;
    return 0;
}


#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <memory>
#include <iomanip>
#include <numeric>
#include <functional>
#include <new>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include <cctype>
#include <ctime>

#define REP(i, n) for(int (i) = 0; i < n; i++)
#define FOR(i, a, n) for(int (i) = a; i < n; i++)
#define FORR(i, a, n) for(int (i) = a; i <= n; i++)
#define for_each(q, s) for(typeof(s.begin()) q=s.begin(); q!=s.end(); q++)
#define sz(n) n.size()
#define pb(n) push_back(n)
#define all(n) n.begin(), n.end()

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 60;

int N, M, A, B, C;
int matrix[MAXN][MAXN], V[MAXN], H[MAXN], buffH[MAXN], buffV[MAXN];

int main(void) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    N = in(), M = in();

    int i, j, k, x;

    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) {
            matrix[i][j] = in();

            H[i] += matrix[i][j];
            V[j] += matrix[i][j];

            buffV[j] = 0;
        }
        buffH[i] = 0;
    }

    for (i = 0; i < N; i++) {
        buffH[i] += H[i];
        if (i > 0) buffH[i] += buffH[i-1];
    }
    for (i = 0; i < M; i++) {
        buffV[i] += V[i];
        if (i > 0) buffV[i] += buffV[i-1];
    }

    int sons[3] = {0};

    sons[0] = in(), sons[1] = in(), sons[2] = in();

    sort (sons, sons + 3);

    int ways = 0;

    for (i = 0; i < N; i++) {
        for (j = i + 1; j < N - 1; j++) {
            int buff[3] = {buffH[i], buffH[j] - buffH[i], buffH[N-1] - buffH[j]};

            sort (buff, buff + 3);

            if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                ways += 1;
            }
        }
    }

    for (i = 0; i < M; i++) {
        for (j = i + 1; j < M - 1; j++) {
            int buff[3] = {buffV[i], buffV[j] - buffV[i], buffV[M-1] - buffV[j]};

            sort (buff, buff + 3);

            if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                ways += 1;
            }
        }
    }

    printf("%d\n", ways);

    return 0;
}


#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <memory>
#include <iomanip>
#include <numeric>
#include <functional>
#include <new>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include <cctype>
#include <ctime>

#define REP(i, n) for(int (i) = 0; i < n; i++)
#define FOR(i, a, n) for(int (i) = a; i < n; i++)
#define FORR(i, a, n) for(int (i) = a; i <= n; i++)
#define for_each(q, s) for(typeof(s.begin()) q=s.begin(); q!=s.end(); q++)
#define sz(n) n.size()
#define pb(n) push_back(n)
#define all(n) n.begin(), n.end()

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 60;

int N, M, A, B, C;
int matrix[MAXN][MAXN], V[MAXN], H[MAXN], buffH[MAXN], buffV[MAXN];

int main(void) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    N = in(), M = in();

    int i, j, k, x;

    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) {
            matrix[i][j] = in();

            H[i] += matrix[i][j];
            V[j] += matrix[i][j];

            buffV[j] = 0;
        }
        buffH[i] = 0;
    }

    for (i = 0; i < N; i++) {
        buffH[i] += H[i];
        if (i > 0) buffH[i] += buffH[i-1];
    }
    for (i = 0; i < M; i++) {
        buffV[i] += V[i];
        if (i > 0) buffV[i] += buffV[i-1];
    }

    int sons[3] = {0};

    sons[0] = in(), sons[1] = in(), sons[2] = in();

    sort (sons, sons + 3);

    int ways = 0;

    for (i = 0; i < N; i++) {
        for (j = i + 1; j < N; j++) {
            int buff[3] = {buffH[i], buffH[j-1] - buffH[i-1], buffH[N-1] - buffH[j]};

            sort (buff, buff + 3);

            if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                ways += 1;
            }
        }
    }

    for (i = 0; i < M; i++) {
        for (j = i + 1; j < M; j++) {
            int buff[3] = {buffV[i], buffV[j-1] - buffV[i-1], buffH[M-1] - buffV[j]};

            sort (buff, buff + 3);

            if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                ways += 1;
            }
        }
    }

    printf("%d\n", ways);

    return 0;
}


#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <memory>
#include <iomanip>
#include <numeric>
#include <functional>
#include <new>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include <cctype>
#include <ctime>

#define REP(i, n) for(int (i) = 0; i < n; i++)
#define FOR(i, a, n) for(int (i) = a; i < n; i++)
#define FORR(i, a, n) for(int (i) = a; i <= n; i++)
#define for_each(q, s) for(typeof(s.begin()) q=s.begin(); q!=s.end(); q++)
#define sz(n) n.size()
#define pb(n) push_back(n)
#define all(n) n.begin(), n.end()

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 60;

int N, M, A, B, C;
int matrix[MAXN][MAXN], V[MAXN], H[MAXN];

int main(void) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    N = in(), M = in();

    int i, j, k, x;

    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) {
            matrix[i][j] = in();

            H[i] += matrix[i][j];
            V[j] += matrix[i][j];
        }
    }

    int sons[3] = {0};

    sons[0] = in(), sons[1] = in(), sons[2] = in();

    sort (sons, sons + 3);

    int ways = 0;

    for (i = 0; i < N; i++) {
        for (j = i + 1; j < N; j++) {
            for (k = j + 1; k <= N; k++) {
                int buff[3] = {0};

                for (x = 0; x < N; x++) {
                    if (x <= i) {
                        buff[0] += H[x];
                    } else if (x > i && x < j) {
                        buff[1] += H[x];
                    } else {
                        buff[2] += H[x];
                    }
                }

                sort (buff, buff + 3);

                if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                    ways += 1;
                }
            }
        }
    }

    for (i = 0; i < M; i++) {
        for (j = i + 1; j < M; j++) {
            for (k = j + 1; k < M; k++) {
                int buff[3] = {0};

                for (x = 0; x < M; x++) {
                    if (x <= i) {
                        buff[0] += V[x];
                    } else if (x >= j && x < k) {
                        buff[1] += V[x];
                    } else if (x >= k) {
                        buff[2] += V[x];
                    }
                }

                sort (buff, buff + 3);

                if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                    ways += 1;
                }
            }
        }
    }

    printf("%d\n", ways);

    return 0;
}


#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <memory>
#include <iomanip>
#include <numeric>
#include <functional>
#include <new>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include <cctype>
#include <ctime>

#define REP(i, n) for(int (i) = 0; i < n; i++)
#define FOR(i, a, n) for(int (i) = a; i < n; i++)
#define FORR(i, a, n) for(int (i) = a; i <= n; i++)
#define for_each(q, s) for(typeof(s.begin()) q=s.begin(); q!=s.end(); q++)
#define sz(n) n.size()
#define pb(n) push_back(n)
#define all(n) n.begin(), n.end()

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 60;

int N, M, A, B, C;
int matrix[MAXN][MAXN], V[MAXN], H[MAXN], buffH[MAXN], buffV[MAXN];

int main(void) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    N = in(), M = in();

    int i, j, k, x;

    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) {
            matrix[i][j] = in();

            H[i] += matrix[i][j];
            V[j] += matrix[i][j];

            buffV[j] = 0;
        }
        buffH[i] = 0;
    }

    for (i = 0; i < N; i++) {
        buffH[i] += H[i];
        if (i > 0) buffH[i] += buffH[i-1];
    }
    for (i = 0; i < M; i++) {
        buffV[i] += V[i];
        if (i > 0) buffV[i] += buffV[i-1];
    }

    int sons[3] = {0};

    sons[0] = in(), sons[1] = in(), sons[2] = in();

    sort (sons, sons + 3);

    int ways = 0;

    for (i = 0; i < N; i++) {
        for (j = i + 1; j < N; j++) {
            int buff[3] = {buffH[i], buffH[j-1] - buffH[i-1], buffH[N-1] - buffH[j]};

            printf("%d %d %d\n", buff[0], buff[1], buff[2]);

            sort (buff, buff + 3);

            if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                ways += 1;
            }
        }
    }

    for (i = 0; i < M; i++) {
        for (j = i + 1; j < M; j++) {
            int buff[3] = {buffV[i], buffV[j-1] - buffV[i-1], buffH[M-1] - buffV[j]};

            sort (buff, buff + 3);

            if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                ways += 1;
            }
        }
    }

    printf("%d\n", ways);

    return 0;
}


#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <memory>
#include <iomanip>
#include <numeric>
#include <functional>
#include <new>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include <cctype>
#include <ctime>

#define REP(i, n) for(int (i) = 0; i < n; i++)
#define FOR(i, a, n) for(int (i) = a; i < n; i++)
#define FORR(i, a, n) for(int (i) = a; i <= n; i++)
#define for_each(q, s) for(typeof(s.begin()) q=s.begin(); q!=s.end(); q++)
#define sz(n) n.size()
#define pb(n) push_back(n)
#define all(n) n.begin(), n.end()

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 60;

int N, M, A, B, C;
int matrix[MAXN][MAXN], V[MAXN], H[MAXN], buffH[MAXN], buffV[MAXN];

int main(void) {
    freopen("input.txt", "w", stdin);
    freopen("output.txt", "w", stdout);
    N = in(), M = in();

    int i, j, k, x;

    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) {
            matrix[i][j] = in();

            H[i] += matrix[i][j];
            V[j] += matrix[i][j];

            buffV[j] = 0;
        }
        buffH[i] = 0;
    }

    for (i = 0; i < N; i++) {
        buffH[i] += H[i];
        if (i > 0) buffH[i] += buffH[i-1];
    }
    for (i = 0; i < M; i++) {
        buffV[i] += V[i];
        if (i > 0) buffV[i] += buffV[i-1];
    }

    int sons[3] = {0};

    sons[0] = in(), sons[1] = in(), sons[2] = in();

    sort (sons, sons + 3);

    int ways = 0;

    for (i = 0; i < N; i++) {
        for (j = i + 1; j < N - 1; j++) {
            int buff[3] = {buffH[i], buffH[j] - buffH[i], buffH[N-1] - buffH[j]};

            sort (buff, buff + 3);

            if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                ways += 1;
            }
        }
    }

    for (i = 0; i < M; i++) {
        for (j = i + 1; j < M - 1; j++) {
            int buff[3] = {buffV[i], buffV[j] - buffV[i], buffH[M-1] - buffV[j]};

            sort (buff, buff + 3);

            if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                ways += 1;
            }
        }
    }

    printf("%d\n", ways);

    return 0;
}


#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <memory>
#include <iomanip>
#include <numeric>
#include <functional>
#include <new>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include <cctype>
#include <ctime>

#define REP(i, n) for(int (i) = 0; i < n; i++)
#define FOR(i, a, n) for(int (i) = a; i < n; i++)
#define FORR(i, a, n) for(int (i) = a; i <= n; i++)
#define for_each(q, s) for(typeof(s.begin()) q=s.begin(); q!=s.end(); q++)
#define sz(n) n.size()
#define pb(n) push_back(n)
#define all(n) n.begin(), n.end()

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 60;

int N, M, A, B, C;
int matrix[MAXN][MAXN], V[MAXN], H[MAXN], buffH[MAXN], buffV[MAXN];

int main(void) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    N = in(), M = in();

    int i, j, k, x;

    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) {
            matrix[i][j] = in();

            H[i] += matrix[i][j];
            V[j] += matrix[i][j];

            buffV[j] = 0;
        }
        buffH[i] = 0;
    }

    for (i = 0; i < N; i++) {
        buffH[i] += H[i];
        if (i > 0) buffH[i] += buffH[i-1];
    }
    for (i = 0; i < M; i++) {
        buffV[i] += V[i];
        if (i > 0) buffV[i] += buffV[i-1];
    }

    int sons[3] = {0};

    sons[0] = in(), sons[1] = in(), sons[2] = in();

    sort (sons, sons + 3);

    int ways = 0;

    for (i = 0; i < N; i++) {
        for (j = i + 1; j < N - 1; j++) {
            int buff[3] = {buffH[i], buffH[j] - buffH[i], buffH[N-1] - buffH[j]};

            sort (buff, buff + 3);

            if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                ways += 1;
            }
        }
    }

    for (i = 0; i < M; i++) {
        for (j = i + 1; j < M - 1; j++) {
            int buff[3] = {buffV[i], buffV[j] - buffV[i], buffH[M-1] - buffV[j]};

            sort (buff, buff + 3);

            if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                ways += 1;
            }
        }
    }

    printf("%d\n", ways);

    return 0;
}


#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <memory>
#include <iomanip>
#include <numeric>
#include <functional>
#include <new>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include <cctype>
#include <ctime>

#define REP(i, n) for(int (i) = 0; i < n; i++)
#define FOR(i, a, n) for(int (i) = a; i < n; i++)
#define FORR(i, a, n) for(int (i) = a; i <= n; i++)
#define for_each(q, s) for(typeof(s.begin()) q=s.begin(); q!=s.end(); q++)
#define sz(n) n.size()
#define pb(n) push_back(n)
#define all(n) n.begin(), n.end()

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 60;

int N, M, A, B, C;
int matrix[MAXN][MAXN], V[MAXN], H[MAXN];

int main(void) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    N = in(), M = in();

    int i, j, k, x;

    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) {
            matrix[i][j] = in();

            H[i] += matrix[i][j];
            V[j] += matrix[i][j];
        }
    }

    int sons[3] = {0};

    sons[0] = in(), sons[1] = in(), sons[2] = in();

    sort (sons, sons + 3);

    int ways = 0;

    for (i = 0; i < N; i++) {
        for (j = i + 1; j < N; j++) {
            for (k = j + 1; k < N; k++) {
                int buff[3] = {0};

                for (x = 0; x < N; x++) {
                    if (x <= i) {
                        buff[0] += H[x];
                    } else if (x >= j && x < k) {
                        buff[1] += H[x];
                    } else if (x >= k) {
                        buff[2] += H[x];
                    }
                }

                sort (buff, buff + 3);

                if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                    ways += 1;
                }
            }
        }
    }

    for (i = 0; i < M; i++) {
        for (j = i + 1; j < M; j++) {
            for (k = j + 1; k < M; k++) {
                int buff[3] = {0};

                for (x = 0; x < M; x++) {
                    if (x <= i) {
                        buff[0] += V[x];
                    } else if (x >= j && x < k) {
                        buff[1] += V[x];
                    } else if (x >= k) {
                        buff[2] += V[x];
                    }
                }

                sort (buff, buff + 3);

                if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                    ways += 1;
                }
            }
        }
    }

    printf("%d\n", ways);

    return 0;
}


#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <memory>
#include <iomanip>
#include <numeric>
#include <functional>
#include <new>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include <cctype>
#include <ctime>

#define REP(i, n) for(int (i) = 0; i < n; i++)
#define FOR(i, a, n) for(int (i) = a; i < n; i++)
#define FORR(i, a, n) for(int (i) = a; i <= n; i++)
#define for_each(q, s) for(typeof(s.begin()) q=s.begin(); q!=s.end(); q++)
#define sz(n) n.size()
#define pb(n) push_back(n)
#define all(n) n.begin(), n.end()

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 60;

int N, M, A, B, C;
int matrix[MAXN][MAXN], V[MAXN], H[MAXN], buffH[MAXN], buffV[MAXN];

int main(void) {
    freopen("input.txt", "w", stdin);
    freopne("output.txt", "w", stdout);
    N = in(), M = in();

    int i, j, k, x;

    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) {
            matrix[i][j] = in();

            H[i] += matrix[i][j];
            V[j] += matrix[i][j];

            buffV[j] = 0;
        }
        buffH[i] = 0;
    }

    for (i = 0; i < N; i++) {
        buffH[i] += H[i];
        if (i > 0) buffH[i] += buffH[i-1];
    }
    for (i = 0; i < M; i++) {
        buffV[i] += V[i];
        if (i > 0) buffV[i] += buffV[i-1];
    }

    int sons[3] = {0};

    sons[0] = in(), sons[1] = in(), sons[2] = in();

    sort (sons, sons + 3);

    int ways = 0;

    for (i = 0; i < N; i++) {
        for (j = i + 1; j < N - 1; j++) {
            int buff[3] = {buffH[i], buffH[j] - buffH[i], buffH[N-1] - buffH[j]};

            sort (buff, buff + 3);

            if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                ways += 1;
            }
        }
    }

    for (i = 0; i < M; i++) {
        for (j = i + 1; j < M - 1; j++) {
            int buff[3] = {buffV[i], buffV[j] - buffV[i], buffH[M-1] - buffV[j]};

            sort (buff, buff + 3);

            if (sons[0] == buff[0] && sons[1] == buff[1] && sons[2] == buff[2]) {
                ways += 1;
            }
        }
    }

    printf("%d\n", ways);

    return 0;
}


#include <algorithm>
#include <cstdio>
#include <iostream>
#include <utility>
using namespace std ;
#define lim 100
#define par pair<int,int>
#define f first
#define s second

int N, M ;
int arr[lim][lim] ;
int rep[3] ;

int subseg( int hA, int vA, int hB, int vB ) {
    int res = arr[hB][vB] ;
    if( hA )
        res -= arr[hA-1][vB] ;
    if( vA )
        res -= arr[hB][vA-1] ;
    if( hA && hB )
        res += arr[hA-1][vA-1] ;
    return res ;
}

int main() {
    int res = 0 ;
    int comp[3] ;

    scanf("%d%d", &N, &M ) ;
    for( int i=0; i<N; i++ ) {
        for( int j=0; j<M; j++ ) {
            scanf("%d", &arr[i][j] ) ;
            if( i )
                arr[i][j] += arr[i-1][j] ;
            if( j )
                arr[i][j] += arr[i][j-1] ;
            if( i && j )
                arr[i][j] -= arr[i-1][j-1] ;
        }
    }
    for( int i=0; i<3; i++  ) {
        scanf("%d", &rep[i] ) ;
    }

    for( int i=0; i<N; i++ ) {
        for( int j=i+1; j<N; j++ ) {
            for( int k=j+1; k<N; k++ ) {
                comp[0] = subseg( i, 0, j-1, M-1 ) ;
                comp[1] = subseg( j, 0, k-1, M-1 ) ;
                comp[2] = subseg( k, 0, N-1, M-1 ) ;
                do {
                    int w ;
                    for( w=0; w<3 && rep[w]==comp[w]; w++ ) ;
                    if( w==3 ) {
                        res ++ ;
                        break ;
                    }
                } while( next_permutation(rep,rep+3)  ) ;
                do {
                }while( next_permutation(rep,rep+3)  ) ;
            }
        }
    }
    for( int i=0; i<M; i++ ) {
        for( int j=i+1; j<M; j++ ) {
            for( int k=j+1; k<M; k++ ) {
                comp[0] = subseg( 0, i, N-1, j-1 ) ;
                comp[1] = subseg( 0, j, N-1, k-1 ) ;
                comp[2] = subseg( 0, k, N-1, M-1 ) ;
                do {
                    int w ;
                    for( w=0; w<3 && rep[w]==comp[w]; w++ ) ;
                    if( w==3 ) {
                        res ++ ;
                        break ;
                    }
                } while( next_permutation(rep,rep+3)  ) ;
                do {
                }while( next_permutation(rep,rep+3)  ) ;
            }
        }
    }

    printf("%d\n", res ) ;

    return 0 ;
}


#include <algorithm>
#include <cstdio>
#include <iostream>
#include <utility>
using namespace std ;
#define lim 100
#define par pair<int,int>
#define f first
#define s second

int N, M ;
int arr[lim][lim] ;
int rep[3] ;

int subseg( int hA, int vA, int hB, int vB ) {
    int res = arr[hB][vB] ;
    if( hA )
        res -= arr[hA-1][vB] ;
    if( vA )
        res -= arr[hB][vA-1] ;
    if( hA && hB )
        res += arr[hA-1][vA-1] ;
    return res ;
}

int main() {
    freopen( "input.txt", "r", stdin ) ;
    freopen( "output.txt", "w", stdout ) ;

    int res = 0 ;
    int comp[3] ;

    scanf("%d%d", &N, &M ) ;
    for( int i=0; i<N; i++ ) {
        for( int j=0; j<M; j++ ) {
            scanf("%d", &arr[i][j] ) ;
            if( i )
                arr[i][j] += arr[i-1][j] ;
            if( j )
                arr[i][j] += arr[i][j-1] ;
            if( i && j )
                arr[i][j] -= arr[i-1][j-1] ;
        }
    }
    for( int i=0; i<3; i++  ) {
        scanf("%d", &rep[i] ) ;
    }
    sort( rep, rep+3 ) ;

    for( int i=1; i<N; i++ ) {
        for( int j=i+1; j<N; j++ ) {
            comp[0] = subseg( 0, 0, i-1, M-1 ) ;
            comp[1] = subseg( i, 0, j-1, M-1 ) ;
            comp[2] = subseg( j, 0, N-1, M-1 ) ;
            do {
                int w ;
                for( w=0; w<3 && rep[w]==comp[w]; w++ ) ;
                if( w==3 ) {
                    res ++ ;
                    break ;
                }
            } while( next_permutation(rep,rep+3)  ) ;
            do {
            }while( next_permutation(rep,rep+3)  ) ;
        }
    }
    for( int i=1; i<M; i++ ) {
        for( int j=i+1; j<M; j++ ) {
            comp[0] = subseg( 0, 0, N-1, i-1 ) ;
            comp[1] = subseg( 0, i, N-1, j-1 ) ;
            comp[2] = subseg( 0, j, N-1, M-1 ) ;
            do {
                int w ;
                for( w=0; w<3 && rep[w]==comp[w]; w++ ) ;
                if( w==3 ) {
                    res ++ ;
                    break ;
                }
            } while( next_permutation(rep,rep+3)  ) ;
            do {
            }while( next_permutation(rep,rep+3)  ) ;
        }
    }

    printf("%d\n", res ) ;

    return 0 ;
}


#include <algorithm>
#include <cstdio>
#include <iostream>
#include <utility>
using namespace std ;
#define lim 100
#define par pair<int,int>
#define f first
#define s second

int N, M ;
int arr[lim][lim] ;
int rep[3] ;

int subseg( int hA, int vA, int hB, int vB ) {
    int res = arr[hB][vB] ;
    if( hA )
        res -= arr[hA-1][vB] ;
    if( vA )
        res -= arr[hB][vA-1] ;
    if( hA && hB )
        res += arr[hA-1][vA-1] ;
    return res ;
}

int main() {
    freopen( "input.txt", "r", stdin ) ;
    freopen( "output.txt", "w", stdout ) ;

    int res = 0 ;
    int comp[3] ;

    scanf("%d%d", &N, &M ) ;
    for( int i=0; i<N; i++ ) {
        for( int j=0; j<M; j++ ) {
            scanf("%d", &arr[i][j] ) ;
            if( i )
                arr[i][j] += arr[i-1][j] ;
            if( j )
                arr[i][j] += arr[i][j-1] ;
            if( i && j )
                arr[i][j] -= arr[i-1][j-1] ;
        }
    }
    for( int i=0; i<3; i++  ) {
        scanf("%d", &rep[i] ) ;
    }

    for( int i=0; i<N; i++ ) {
        for( int j=i+1; j<N; j++ ) {
            for( int k=j+1; k<N; k++ ) {
                comp[0] = subseg( i, 0, j-1, M-1 ) ;
                comp[1] = subseg( j, 0, k-1, M-1 ) ;
                comp[2] = subseg( k, 0, N-1, M-1 ) ;
                do {
                    int w ;
                    for( w=0; w<3 && rep[w]==comp[w]; w++ ) ;
                    if( w==3 ) {
                        res ++ ;
                        break ;
                    }
                } while( next_permutation(rep,rep+3)  ) ;
                do {
                }while( next_permutation(rep,rep+3)  ) ;
            }
        }
    }
    for( int i=0; i<M; i++ ) {
        for( int j=i+1; j<M; j++ ) {
            for( int k=j+1; k<M; k++ ) {
                comp[0] = subseg( 0, i, N-1, j-1 ) ;
                comp[1] = subseg( 0, j, N-1, k-1 ) ;
                comp[2] = subseg( 0, k, N-1, M-1 ) ;
                do {
                    int w ;
                    for( w=0; w<3 && rep[w]==comp[w]; w++ ) ;
                    if( w==3 ) {
                        res ++ ;
                        break ;
                    }
                } while( next_permutation(rep,rep+3)  ) ;
                do {
                }while( next_permutation(rep,rep+3)  ) ;
            }
        }
    }

    printf("%d\n", res ) ;

    return 0 ;
}


#include <algorithm>
#include <cstdio>
#include <iostream>
#include <utility>
using namespace std ;
#define lim 100
#define par pair<int,int>
#define f first
#define s second

int N, M ;
int arr[lim][lim] ;
int rep[3] ;

int subseg( int hA, int vA, int hB, int vB ) {
    int res = arr[hB][vB] ;
    if( hA )
        res -= arr[hA-1][vB] ;
    if( vA )
        res -= arr[hB][vA-1] ;
    if( hA && hB )
        res += arr[hA-1][vA-1] ;
    return res ;
}

int main() {
    freopen( "input.txt", "r", stdin ) ;
    freopen( "output.txt", "w", stdout ) ;

    int res = 0 ;
    int comp[3] ;

    scanf("%d%d", &N, &M ) ;
    for( int i=0; i<N; i++ ) {
        for( int j=0; j<M; j++ ) {
            scanf("%d", &arr[i][j] ) ;
            if( i )
                arr[i][j] += arr[i-1][j] ;
            if( j )
                arr[i][j] += arr[i][j-1] ;
            if( i && j )
                arr[i][j] -= arr[i-1][j-1] ;
        }
    }
    for( int i=0; i<3; i++  ) {
        scanf("%d", &rep[i] ) ;
    }
    sort( rep, rep+3 ) ;

    for( int i=0; i<N; i++ ) {
        for( int j=i+1; j<N; j++ ) {
            for( int k=j+1; k<N; k++ ) {
                comp[0] = subseg( i, 0, j-1, M-1 ) ;
                comp[1] = subseg( j, 0, k-1, M-1 ) ;
                comp[2] = subseg( k, 0, N-1, M-1 ) ;

                do {
                    int w ;
                    for( w=0; w<3 && rep[w]==comp[w]; w++ ) ;
                    if( w==3 ) {
                        res ++ ;
                        break ;
                    }
                } while( next_permutation(rep,rep+3)  ) ;
                do {
                }while( next_permutation(rep,rep+3)  ) ;
            }
        }
    }
    for( int i=0; i<M; i++ ) {
        for( int j=i+1; j<M; j++ ) {
            for( int k=j+1; k<M; k++ ) {
                comp[0] = subseg( 0, i, N-1, j-1 ) ;
                comp[1] = subseg( 0, j, N-1, k-1 ) ;
                comp[2] = subseg( 0, k, N-1, M-1 ) ;
                do {
                    int w ;
                    for( w=0; w<3 && rep[w]==comp[w]; w++ ) ;
                    if( w==3 ) {
                        res ++ ;
                        break ;
                    }
                } while( next_permutation(rep,rep+3)  ) ;
                do {
                }while( next_permutation(rep,rep+3)  ) ;
            }
        }
    }

    printf("%d\n", res ) ;

    return 0 ;
}


#include <algorithm>
#include <cstdio>
#include <iostream>
#include <utility>
using namespace std ;
#define lim 100
#define par pair<int,int>
#define f first
#define s second

int N, M ;
int arr[lim][lim] ;
int rep[3] ;

int subseg( int hA, int vA, int hB, int vB ) {
    int res = arr[hB][vB] ;
    if( hA )
        res -= arr[hA-1][vB] ;
    if( vA )
        res -= arr[hB][vA-1] ;
    if( hA && hB )
        res += arr[hA-1][vA-1] ;
    return res ;
}

int main() {
    //freopen( "input.txt", "r", stdin ) ;
    //freopen( "output.txt", "w", stdout ) ;

    int res = 0 ;
    int comp[3] ;

    scanf("%d%d", &N, &M ) ;
    for( int i=0; i<N; i++ ) {
        for( int j=0; j<M; j++ ) {
            scanf("%d", &arr[i][j] ) ;
            if( i )
                arr[i][j] += arr[i-1][j] ;
            if( j )
                arr[i][j] += arr[i][j-1] ;
            if( i && j )
                arr[i][j] -= arr[i-1][j-1] ;
        }
    }
    for( int i=0; i<3; i++  ) {
        scanf("%d", &rep[i] ) ;
    }
    sort( rep, rep+3 ) ;

    for( int i=1; i<N; i++ ) {
        for( int j=i+1; j<N; j++ ) {
            comp[0] = subseg( 0, 0, i-1, M-1 ) ;
            comp[1] = subseg( i, 0, j-1, M-1 ) ;
            comp[2] = subseg( j, 0, N-1, M-1 ) ;
            do {
                int w ;
                for( w=0; w<3 && rep[w]==comp[w]; w++ ) ;
                if( w==3 ) {
                    res ++ ;
                    break ;
                }
            } while( next_permutation(rep,rep+3)  ) ;
            do {
            }while( next_permutation(rep,rep+3)  ) ;
        }
    }
    for( int i=1; i<M; i++ ) {
        for( int j=i+1; j<M; j++ ) {
            comp[0] = subseg( 0, 0, N-1, i-1 ) ;
            comp[1] = subseg( 0, i, N-1, j-1 ) ;
            comp[2] = subseg( 0, j, N-1, M-1 ) ;
            do {
                int w ;
                for( w=0; w<3 && rep[w]==comp[w]; w++ ) ;
                if( w==3 ) {
                    res ++ ;
                    break ;
                }
            } while( next_permutation(rep,rep+3)  ) ;
            do {
            }while( next_permutation(rep,rep+3)  ) ;
        }
    }

    printf("%d\n", res ) ;

    return 0 ;
}


#include <iostream>
#include <map>
#include <algorithm>
#include <sstream>
#include <vector>
using namespace std;

int main() {
    int m, n;
    cin >> m >> n;
    int z[m][n];
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cin >> z[i][j];
        }
    }
    int x[3];
    cin >> x[0] >> x[1] >> x[2];
    int ans = 0;

    for (int i = 1; i < n-1; i++) {
        for (int j = i+1; j < n; j++) {
            int y[3] = {0,0,0};
            for (int k = 0; k < m; k++) {
                for (int l = 0; l < n; l++) {
                    if (l < i) y[0] += z[k][l];
                    else if (l < j) y[1] += z[k][l];
                    else y[2] += z[k][l];
                }
            }

            sort(y,y+3);
            do {
                if (x[0] == y[0] and x[1] == y[2] and x[2] == y[2]) {
                    ans++;
                    break;
                }
            } while (next_permutation(y,y+3));

        }
    }

    for (int i = 1; i < m-1; i++) {
        for (int j = i+1; j < m; j++) {
            int y[3] = {0,0,0};
            for (int k = 0; k < m; k++) {
                for (int l = 0; l < n; l++) {
                    if (k < i) y[0] += z[k][l];
                    else if (k < j) y[1] += z[k][l];
                    else y[2] += z[k][l];
                }
            }

            sort(y,y+3);
            do {
                if (x[0] == y[0] and x[1] == y[2] and x[2] == y[2]) {
                    ans++;
                    break;
                }
            } while (next_permutation(y,y+3));

        }
    }

    cout << ans;
}


#include <iostream>
#include <map>
#include <algorithm>
#include <sstream>
#include <vector>
#include <cstdio>
using namespace std;

int main() {
    #ifdef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int m, n;
    cin >> m >> n;
    int z[m][n];
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cin >> z[i][j];
        }
    }
    int x[3];
    cin >> x[0] >> x[1] >> x[2];
    sort(x,x+3);
    int ans = 0;

    for (int i = 1; i < n-1; i++) {
        for (int j = i+1; j < n; j++) {
            int y[3] = {0,0,0};
            for (int k = 0; k < m; k++) {
                for (int l = 0; l < n; l++) {
                    if (l < i) y[0] += z[k][l];
                    else if (l < j) y[1] += z[k][l];
                    else y[2] += z[k][l];
                }
            }

            sort(y,y+3);
            do {
                if (x[0] == y[0] and x[1] == y[1] and x[2] == y[2]) {
                    ans++;
                    break;
                }
            } while (next_permutation(y,y+3));

        }
    }

    for (int i = 1; i < m-1; i++) {
        for (int j = i+1; j < m; j++) {
            int y[3] = {0,0,0};
            for (int k = 0; k < m; k++) {
                for (int l = 0; l < n; l++) {
                    if (k < i) y[0] += z[k][l];
                    else if (k < j) y[1] += z[k][l];
                    else y[2] += z[k][l];
                }
            }

            sort(y,y+3);

            if (x[0] == y[0] and x[1] == y[1] and x[2] == y[2])
            ans++;

        }
    }

    cout << ans;
}


#include <iostream>
#include <map>
#include <algorithm>
#include <sstream>
#include <vector>
#include <cstdio>
using namespace std;

int main() {
    #ifdef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int m, n;
    cin >> m >> n;
    int z[m][n];
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cin >> z[i][j];
        }
    }
    int x[3];
    cin >> x[0] >> x[1] >> x[2];
    sort(x,x+3);
    int ans = 0;

    for (int i = 1; i < n-1; i++) {
        for (int j = i+1; j < n; j++) {
            int y[3] = {0,0,0};
            for (int k = 0; k < m; k++) {
                for (int l = 0; l < n; l++) {
                    if (l < i) y[0] += z[k][l];
                    else if (l < j) y[1] += z[k][l];
                    else y[2] += z[k][l];
                }
            }

            sort(y,y+3);
            do {
                if (x[0] == y[0] and x[1] == y[2] and x[2] == y[2]) {
                    ans++;
                    break;
                }
            } while (next_permutation(y,y+3));

        }
    }

    for (int i = 1; i < m-1; i++) {
        for (int j = i+1; j < m; j++) {
            int y[3] = {0,0,0};
            for (int k = 0; k < m; k++) {
                for (int l = 0; l < n; l++) {
                    if (k < i) y[0] += z[k][l];
                    else if (k < j) y[1] += z[k][l];
                    else y[2] += z[k][l];
                }
            }

            sort(y,y+3);

            if (x[0] == y[0] and x[1] == y[1] and x[2] == y[2])
            ans++;

        }
    }

    cout << ans;
}


#include <iostream>
#include <map>
#include <algorithm>
#include <sstream>
#include <vector>
#include <cstdio>
using namespace std;

int main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int m, n;
    cin >> m >> n;
    int z[m][n];
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cin >> z[i][j];
        }
    }
    int x[3];
    cin >> x[0] >> x[1] >> x[2];
    int ans = 0;

    for (int i = 1; i < n-1; i++) {
        for (int j = i+1; j < n; j++) {
            int y[3] = {0,0,0};
            for (int k = 0; k < m; k++) {
                for (int l = 0; l < n; l++) {
                    if (l < i) y[0] += z[k][l];
                    else if (l < j) y[1] += z[k][l];
                    else y[2] += z[k][l];
                }
            }

            sort(y,y+3);
            do {
                if (x[0] == y[0] and x[1] == y[2] and x[2] == y[2]) {
                    ans++;
                    break;
                }
            } while (next_permutation(y,y+3));

        }
    }

    for (int i = 1; i < m-1; i++) {
        for (int j = i+1; j < m; j++) {
            int y[3] = {0,0,0};
            for (int k = 0; k < m; k++) {
                for (int l = 0; l < n; l++) {
                    if (k < i) y[0] += z[k][l];
                    else if (k < j) y[1] += z[k][l];
                    else y[2] += z[k][l];
                }
            }

            sort(y,y+3);
            do {
                if (x[0] == y[0] and x[1] == y[2] and x[2] == y[2]) {
                    ans++;
                    break;
                }
            } while (next_permutation(y,y+3));

        }
    }

    cout << ans;
}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 150;

int a[ MAXN ][ MAXN ];
int sumi[ MAXN ];
int sumj[ MAXN ];

int n, m;

#define sint( x ) scanf( "%d", &x )
#define pint( x ) printf( "%d ", x )
#define pintn( x ) printf( "%d\n", x );
#define FOR( i, a, b ) for( (i) = (a); (i) < (b); ++(i))
int main()
{
freopen( "input.txt", "r", stdin );
freopen( "output.txt", "w", stdout );

sint( n );
sint( m );

int i, j;
FOR( i, 0, n )
FOR( j, 0, m )
{
sint( a[ i ][ j ] );
sumi[ i ] += a[ i ][ j ];
sumj[ j ] += a[ i ][ j ];
}

int s[ 3 ];

sint( s[ 0 ] );
sint( s[ 1 ] );
sint( s[ 2 ] );

sort( s, s + 3 );

FOR( i, 1, n )
sumi[ i ] += sumi[ i - 1 ];

FOR( j, 1, m )
sumj[ j ] += sumj[ j - 1 ];

int k;
int ans = 0;

int tos[ 3 ];

FOR( i, 0, n - 1 )
FOR( k, i + 1, n - 1 )
{
tos[ 0 ] = sumi[ i ];
tos[ 1 ] = sumi[ k ] - sumi[ i ];
tos[ 2 ] = sumi[ n - 1 ] - sumi[ k ];
sort( tos, tos + 3 );
if( tos[ 0 ] == s[ 0 ] && tos[ 1 ] == s[ 1 ] && tos[ 2 ] == s[ 2 ] )
++ans;
}

FOR( j, 0, m - 1 )
FOR( k, j + 1, m - 1 )
{
tos[ 0 ] = sumj[ j ];
tos[ 1 ] = sumj[ k ] - sumj[ j ];
tos[ 2 ] = sumj[ m - 1 ] - sumj[ k ];
sort( tos, tos + 3 );
if( tos[ 0 ] == s[ 0 ] && tos[ 1 ] == s[ 1 ] && tos[ 2 ] == s[ 2 ] )
++ans;
}

pintn( ans );
return 0;

}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 150;

int a[ MAXN ][ MAXN ];
int sumi[ MAXN ];
int sumj[ MAXN ];

int n, m;

#define scanint( x ) scanf( "%d", &x )
#define FOR( i, a, b ) for( (i) = (a); (i) < (b); ++(i))
int main()
{
freopen( "input.txt", "r", stdin );
freopen( "output.txt", "w", stdout );

scanint( n );
scanint( m );

int i, j;
FOR( i, 0, n )
FOR( j, 0, m )
{
scanint( a[ i ][ j ] );
sumi[ i ] += a[ i ][ j ];
sumj[ j ] += a[ i ][ j ];
}

int s[ 3 ];

scanint( s[ 0 ] );
scanint( s[ 1 ] );
scanint( s[ 2 ] );

sort( s, s + 3 );

FOR( i, 1, n )
sumi[ i ] += sumi[ i - 1 ];

FOR( j, 1, m )
sumj[ j ] += sumj[ j - 1 ];

int k;
int ans = 0;

int tos[ 3 ];

FOR( i, 0, n - 1 )
FOR( k, i + 1, n - 1 )
{
tos[ 0 ] = sumi[ i ];
tos[ 1 ] = sumi[ k ] - sumi[ i ];
tos[ 2 ] = sumi[ n - 1 ] - sumi[ k ];
sort( tos, tos + 3 );
if( tos[ 0 ] == s[ 0 ] && tos[ 1 ] == s[ 1 ] && tos[ 2 ] == s[ 2 ] )
++ans;
}

FOR( j, 0, m - 1 )
FOR( k, j + 1, m - 1 )
{
tos[ 0 ] = sumj[j ];
tos[ 1 ] = sumj[ k ] - sumi[ j ];
tos[ 2 ] = sumj[ m - 1 ] - sumi[ k ];
sort( tos, tos + 3 );
if( tos[ 0 ] == s[ 0 ] && tos[ 1 ] == s[ 1 ] && tos[ 2 ] == s[ 2 ] )
++ans;
}

printf( "%d\n", ans );
return 0;

}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 150;

int a[ MAXN ][ MAXN ];
int sumi[ MAXN ];
int sunj[ MAXN ];

int n, m;

#define scanint( x ) scanf( "%d", &x )
#define FOR( i, a, b ) for( (i) = (a); (i) < (b); ++(i))
int main()
{
freopen( "input.txt", "r", stdin );
freopen( "output.txt", "w", stdout );

scanint( n );
scanint( m );

int i, j;
FOR( i, 0, n )
FOR( j, 0, m )
{
scanint( a[ i ][ j ] );
sumi[ i ] += a[ i ][ j ];
sumj[ j ] += a[ i ][ j ];
}

int s[ 3 ];

scanint( s[ 0 ] );
scanint( s[ 1 ] );
scanint( s[ 2 ] );

FOR( i, 1, n )
sumi[ i ] += sumi[ i - 1 ];

FOR( j, 1, m )
sumj[ j ] += sumj[ j - 1 ];

int k;
int ans = 0;

int tos[ 3 ];

FOR( i, 0, n - 1 )
FOR( k, i + 1, n - 1 )
{
tos[ 0 ] = sumi[ i ];
tos[ 1 ] = sumi[ k ] - sumi[ i ];
tos[ 2 ] = sumi[ n - 1 ] - sumi[ k ];
sort( tos, tos + 3 );
if( tos[ 0 ] == s[ 0 ] && tos[ 1 ] == s[ 1 ] && tos[ 2 ] == s[ 2 ] )
++ans;
}

printf( "%d\n", ans );
return 0;

}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 150;

int a[ MAXN ][ MAXN ];
int sumi[ MAXN ];
int sumj[ MAXN ];

int n, m;

#define scanint( x ) scanf( "%d", &x )
#define FOR( i, a, b ) for( (i) = (a); (i) < (b); ++(i))
int main()
{
freopen( "input.txt", "r", stdin );
freopen( "output.txt", "w", stdout );

scanint( n );
scanint( m );

int i, j;
FOR( i, 0, n )
FOR( j, 0, m )
{
scanint( a[ i ][ j ] );
sumi[ i ] += a[ i ][ j ];
sumj[ j ] += a[ i ][ j ];
}

int s[ 3 ];

scanint( s[ 0 ] );
scanint( s[ 1 ] );
scanint( s[ 2 ] );

FOR( i, 1, n )
sumi[ i ] += sumi[ i - 1 ];

FOR( j, 1, m )
sumj[ j ] += sumj[ j - 1 ];

int k;
int ans = 0;

int tos[ 3 ];

FOR( i, 0, n - 1 )
FOR( k, i + 1, n - 1 )
{
tos[ 0 ] = sumi[ i ];
tos[ 1 ] = sumi[ k ] - sumi[ i ];
tos[ 2 ] = sumi[ n - 1 ] - sumi[ k ];
sort( tos, tos + 3 );
if( tos[ 0 ] == s[ 0 ] && tos[ 1 ] == s[ 1 ] && tos[ 2 ] == s[ 2 ] )
++ans;
}

FOR( j, 0, m - 1 )
FOR( k, j + 1, m - 1 )
{
tos[ 0 ] = sumj[j ];
tos[ 1 ] = sumj[ k ] - sumi[ j ];
tos[ 2 ] = sumj[ n - 1 ] - sumi[ k ];
sort( tos, tos + 3 );
if( tos[ 0 ] == s[ 0 ] && tos[ 1 ] == s[ 1 ] && tos[ 2 ] == s[ 2 ] )
++ans;
}

printf( "%d\n", ans );
return 0;

}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 150;

int a[ MAXN ][ MAXN ];
int sumi[ MAXN ];
int sunj[ MAXN ];

int n, m;

#define scanint( x ) scanf( "%d", &x )
#define FOR( i, a, b ) for( (i) = (a); (i) < (b); ++(i))
int main()
{
scanint( n );
scanint( m );

int i, j;
FOR( i, 0, n )
FOR( j, 0, m )
{
scanint( a[ i ][ j ];
sumi[ i ] += a[ i ][ j ];
sumj[ j ] += a[ i ][ j ];
}

int s[ 3 ];

scanint( s[ 0 ] );
scanint( s[ 1 ] );
scanint( s[ 2 ] );

FOR( i, 1, n )
sumi[ i ] += sumi[ i - 1 ];

FOR( j, 1, m )
sumj[ j ] += sumj[ j - 1 ];

int k;
int ans = 0;

int tos[ 3 ];

FOR( i, 0, n - 1 )
FOR( k, i + 1, n - 1 )
{
tos[ 0 ] = sumi[ i ];
tos[ 1 ] = sumi[ k ] - sumi[ i ];
tos[ 2 ] = sumi[ n - 1 ] - sumi[ k ];
sort( tos, tos + 3 );
if( tos[ 0 ] == s[ 0 ] && tos[ 1 ] == s[ 1 ] && tos[ 2 ] == s[ 2 ] )
++ans;
}

printf( "%d\n", ans );
return 0;

}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 150;

int a[ MAXN ][ MAXN ];
int sumi[ MAXN ];
int sumj[ MAXN ];

int n, m;

#define scanint( x ) scanf( "%d", &x )
#define FOR( i, a, b ) for( (i) = (a); (i) < (b); ++(i))
int main()
{
freopen( "input.txt", "r", stdin );
freopen( "output.txt", "w", stdout );

scanint( n );
scanint( m );

int i, j;
FOR( i, 0, n )
FOR( j, 0, m )
{
scanint( a[ i ][ j ] );
sumi[ i ] += a[ i ][ j ];
sumj[ j ] += a[ i ][ j ];
}

int s[ 3 ];

scanint( s[ 0 ] );
scanint( s[ 1 ] );
scanint( s[ 2 ] );

sort( s, s + 3 );

FOR( i, 1, n )
sumi[ i ] += sumi[ i - 1 ];

FOR( j, 1, m )
sumj[ j ] += sumj[ j - 1 ];

int k;
int ans = 0;

int tos[ 3 ];

FOR( i, 0, n - 1 )
FOR( k, i + 1, n - 1 )
{
tos[ 0 ] = sumi[ i ];
tos[ 1 ] = sumi[ k ] - sumi[ i ];
tos[ 2 ] = sumi[ n - 1 ] - sumi[ k ];
sort( tos, tos + 3 );
if( tos[ 0 ] == s[ 0 ] && tos[ 1 ] == s[ 1 ] && tos[ 2 ] == s[ 2 ] )
++ans;
}

FOR( j, 0, m - 1 )
FOR( k, j + 1, m - 1 )
{
tos[ 0 ] = sumj[j ];
tos[ 1 ] = sumj[ k ] - sumi[ j ];
tos[ 2 ] = sumj[ n - 1 ] - sumi[ k ];
sort( tos, tos + 3 );
if( tos[ 0 ] == s[ 0 ] && tos[ 1 ] == s[ 1 ] && tos[ 2 ] == s[ 2 ] )
++ans;
}

printf( "%d\n", ans );
return 0;

}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 150;

int a[ MAXN ][ MAXN ];
int sumi[ MAXN ];
int sumj[ MAXN ];

int n, m;

#define sint( x ) scanf( "%d", &x )
#define pint( x ) printf( "%d ", &x )
#define pintn( x ) printf( "%d\n", &x );
#define FOR( i, a, b ) for( (i) = (a); (i) < (b); ++(i))
int main()
{
freopen( "input.txt", "r", stdin );
freopen( "output.txt", "w", stdout );

sint( n );
sint( m );

int i, j;
FOR( i, 0, n )
FOR( j, 0, m )
{
sint( a[ i ][ j ] );
sumi[ i ] += a[ i ][ j ];
sumj[ j ] += a[ i ][ j ];
}

int s[ 3 ];

sint( s[ 0 ] );
sint( s[ 1 ] );
sint( s[ 2 ] );

sort( s, s + 3 );

FOR( i, 1, n )
sumi[ i ] += sumi[ i - 1 ];

FOR( j, 1, m )
sumj[ j ] += sumj[ j - 1 ];

int k;
int ans = 0;

int tos[ 3 ];

FOR( i, 0, n - 1 )
FOR( k, i + 1, n - 1 )
{
tos[ 0 ] = sumi[ i ];
tos[ 1 ] = sumi[ k ] - sumi[ i ];
tos[ 2 ] = sumi[ n - 1 ] - sumi[ k ];
sort( tos, tos + 3 );
if( tos[ 0 ] == s[ 0 ] && tos[ 1 ] == s[ 1 ] && tos[ 2 ] == s[ 2 ] )
++ans;
}

FOR( j, 0, m - 1 )
FOR( k, j + 1, m - 1 )
{
tos[ 0 ] = sumj[ j ];
tos[ 1 ] = sumj[ k ] - sumj[ j ];
tos[ 2 ] = sumj[ m - 1 ] - sumj[ k ];
sort( tos, tos + 3 );
if( tos[ 0 ] == s[ 0 ] && tos[ 1 ] == s[ 1 ] && tos[ 2 ] == s[ 2 ] )
++ans;
}

pintn( ans );
return 0;

}


#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 150;

int a[ MAXN ][ MAXN ];
int sumi[ MAXN ];
int sunj[ MAXN ];

int n, m;

#define scanint( x ) scanf( "%d", &x )
#define FOR( i, a, b ) for( (i) = (a); (i) < (b); ++(i))
int main()
{
freopen( "input.txt", "r", stdin );
freopen( "output.txt", "w", stdout );

scanint( n );
scanint( m );

int i, j;
FOR( i, 0, n )
FOR( j, 0, m )
{
scanint( a[ i ][ j ];
sumi[ i ] += a[ i ][ j ];
sumj[ j ] += a[ i ][ j ];
}

int s[ 3 ];

scanint( s[ 0 ] );
scanint( s[ 1 ] );
scanint( s[ 2 ] );

FOR( i, 1, n )
sumi[ i ] += sumi[ i - 1 ];

FOR( j, 1, m )
sumj[ j ] += sumj[ j - 1 ];

int k;
int ans = 0;

int tos[ 3 ];

FOR( i, 0, n - 1 )
FOR( k, i + 1, n - 1 )
{
tos[ 0 ] = sumi[ i ];
tos[ 1 ] = sumi[ k ] - sumi[ i ];
tos[ 2 ] = sumi[ n - 1 ] - sumi[ k ];
sort( tos, tos + 3 );
if( tos[ 0 ] == s[ 0 ] && tos[ 1 ] == s[ 1 ] && tos[ 2 ] == s[ 2 ] )
++ans;
}

printf( "%d\n", ans );
return 0;

}


#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    scanf("%d", &n);

    pair<int, int>* x = new pair<int, int>[n * 2];
    for (int i = 0; i < n * 2; ++i)
    {
        scanf("%d", &x[i].first);
        x[i].second = i + 1;
    }

    sort(x, x + n * 2);
    bool possible = true;
    for (int i = 0; i < n * 2; i += 2)
    {
        if (x[i].first != x[i+1].first)
        {
            possible = false;
            break;
        }
    }

    if (possible)
    {
        for (int i = 0; i < n * 2; i += 2)
        {
            printf("%d %d\n", x[i].second, x[i+1].second);
        }
    }
    else
    {
        printf("-1\n");
    }

    delete[] x;
    return 0;
}


#include<vector>
#include<iostream>
#include<fstream>
using   namespace   std;
int main(void)
{
    ifstream    fin("input.txt");
    ofstream    fout("output.txt");
    long    n,m,i;
    vector<long>V[5000];
    for(bool    f;fin>>n;){
        for(n<<=1,i=0;n^i;V[m-1].push_back(++i))    fin>>m;
        for(i=5000;i--&&!(1&V[i].size()););
        if(~i)  fout<<"-1\n";
        else    while(f=0,5000^++i) for(long    e:V[i]) fout<<e<<((f=!f)?' ':'\n');
        for(;n--;V[n].clear());
    }
    return  0;
}


#include<vector>
#include<iostream>
#include<fstream>
using	namespace	std;
int	main(void)
{
	ifstream	fin("input.txt");
	ofstream	fout("output.txt");
	long	n,m,i;
	vector<long>V[5000];
	for(bool	f;fin>>n;){
		for(n<<=1,i=0;n^i;V[m-1].push_back(++i))	fin>>m;
		for(i=5000;i--&&!(1&V[i].size()););
		if(~i)	fout<<"-1\n";
		else	while(f=0,5000^++i)	for(long	e:V[i])	fout<<e<<((f=!f)?' ':'\n');
		for(n=5000;n--;V[n].clear());
	}
	return	0;
}


#include<vector>
#include<algorithm>
#include<iostream>
#include<fstream>
using	namespace	std;
bool	pf(const	vector<long>&E){return	1&E.size();}
int	main(void)
{
	ifstream	fin("input.txt");
	ofstream	fout("output.txt");
	long	n,m,i;
	vector<long>V[5000];
	for(bool	f=0;fin>>n;){
		for(n<<=1,i=0;n^i;V[m-1].push_back(++i))	fin>>m;
		if(find_if(V,5000+V,pf)!=5000+V)	fout<<"-1\n";
		else	for(const	vector<long>&E:V)
			for(long	e:E)	fout<<e<<((f=!f)?' ':'\n');
		for(vector<long>&E:V)	E.clear();
	}
	return	0;
}


#include<vector>
#include<functional>
#include<algorithm>
#include<iostream>
#include<fstream>
using	namespace	std;
int	main(void)
{
	ifstream	fin("input.txt");
	ofstream	fout("output.txt");
	long	n,m,i;
	vector<long>V[5000];
	auto	pf=[](const	vector<long>&V)->bool{return	1&V.size();};
	for(bool	f=0;fin>>n;){
		for(n<<=1,i=0;n^i;V[m-1].push_back(++i))	fin>>m;
		if(find_if(V,5000+V,pf)!=5000+V)	fout<<"-1\n";
		else	for(const	vector<long>&E:V)
			for(long	e:E)	fout<<e<<((f=!f)?' ':'\n');
		for(vector<long>&E:V)	E.clear();
	}
	return	0;
}


#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<queue>
#define inf 1600005
#define M 40
#define N 100005
#define maxn 2000005
#define eps 1e-7
#define zero(a) fabs(a)<eps
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define MOD 1000000007
#define lson step<<1
#define rson step<<1|1
#define sqr(a) ((a)*(a))
#define Key_value ch[ch[root][1]][0]
#define test puts("OK");
#define pi acos(-1.0)
#define lowbit(x) ((x)&(-(x)))
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define vi vector<int>
using namespace std;
pair<int,int>a[300005];
int n;
int main(){
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    cin>>n;n*=2;
    for(int i=0;i<n;i++) {
        int k;cin>>k;
        a[i].first=k;;a[i].second=i+1;
    }
    sort(a,a+n);
    if(n&1){puts("-1");return 0;}
    for(int i=0;i<n;i+=2)
        if(a[i].first!=a[i+1].first)
            {
                puts("-1");
                return 0;
            }
    for(int i=0;i<n;i+=2)
        printf("%d %d\n",a[i].second,a[i+1].second);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<queue>
#define inf 1600005
#define M 40
#define N 100005
#define maxn 2000005
#define eps 1e-7
#define zero(a) fabs(a)<eps
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define MOD 1000000007
#define lson step<<1
#define rson step<<1|1
#define sqr(a) ((a)*(a))
#define Key_value ch[ch[root][1]][0]
#define test puts("OK");
#define pi acos(-1.0)
#define lowbit(x) ((x)&(-(x)))
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define vi vector<int>
using namespace std;
pair<int,int>a[1000005];
int n;
int main(){
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    cin>>n;n*=2;
    for(int i=0;i<n;i++) {
        int k;cin>>k;
        a[i].first=k;;a[i].second=i+1;
    }
    sort(a,a+n);
    if(n&1){puts("-1");return 0;}
    for(int i=0;i<n;i+=2)
        if(a[i].first!=a[i+1].first)
            {
                puts("-1");
                return 0;
            }
    for(int i=0;i<n;i+=2)
        printf("%d %d\n",a[i].second,a[i+1].second);
    return 0;
}


#include <cstdio>
#include <iostream>

#include <vector>
#include <list>
#include <string>

#include <algorithm>
#include <functional>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;

int n;
vector<int> xs[5050];

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    scanf("%d", &n);
    for(int i = 0; i < 2 * n; ++i) {
        int x;
        scanf("%d", &x);
        xs[x].push_back(i + 1);
    }

    for(int i = 1; i <= 5000; ++i)
        if(xs[i].size() % 2 != 0) {
            cout << -1 << endl;
            return 0;
        }

    for(int i = 1; i <= 5000; ++i)
        for(size_t j = 0; j < xs[i].size(); j += 2)
            cout << xs[i][j] << ' ' << xs[i][j + 1] << endl;

    return 0;
}


#include <cstdio>
#include <vector>

const int Limit = 5000;

std::vector<int> nums_[Limit + 1];

bool check() {
  for (int i = 1; i <= Limit; ++i) {
    if (nums_[i].size() > 0 && nums_[i].size() % 2 != 0) return false;
  }
  return true;
}

int main() {
  std::freopen("input.txt", "r", stdin);
  std::freopen("output.txt", "w", stdout);

  int n; std::scanf("%d", &n);
  for (int i = 1; i <= n * 2; ++i) {
    int a; std::scanf("%d", &a);
    nums_[a].push_back(i);
  }

  if (!check()) {
    std::printf("-1\n");
    return 0;
  }

  for (int i = 1; i <= Limit; ++i) {
    for (int j = 0; j < nums_[i].size(); j += 2) {
      std::printf("%d %d\n", nums_[i][j], nums_[i][j + 1]);
    }
  }
  return 0;
}


#include<set>
#include<cmath>
#include<stack>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<numeric>
#include<vector>
#include<ctime>
#include<queue>
#include<list>
#include<map>
#define pi acos(-1)
#define INF 0x7fffffff
#define clr(x)  memset(x,0,sizeof(x));
#define clrto(x,siz,y)  for(int xx=0;xx<=siz;xx++)  x[xx]=y;
#define clrset(x,siz)  for(int xx=0;xx<=siz;xx++)  x[xx]=xx;
#define clrvec(x,siz) for(int xx=0;x<=siz;xx++)  x[xx].clear();
#define fop   freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define myprogram By_135678942570
#define clrcpy(x,siz,y)  for(int xx=0;xx<siz;xx++)  x[xx]=y[xx];
using namespace std;
vector<int>listt[5055];
int main()
{
    int n;
    fop;
    while(scanf("%d",&n)>0)
    {
        for(int i=0;i<2*n;i++)
        {
            int t;
            cin>>t;
            listt[t].push_back(i+1);
        }
        for(int i=1;i<=5000;i++)
        {
            if(listt[i].size()%2==1)
            {
                puts("-1");
                return 0;
            }
        }
        for(int i=1;i<=5000;i++)
        {
            for(int j=0;j<listt[i].size();j+=2)
            {
                printf("%d %d\n",listt[i][j],listt[i][j+1]);
            }
        }
    }
}


    #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <cmath>
#include<fstream>
using namespace std;
const int O=2e9;
const double E=1e-9;
const double pi = 3.1415926536;
int DX[]={1,-1,0,0};
int DY[]={0,0,1,-1};

        vector<pair<int,int> >p;

int main()
{
     freopen("input.txt","r",stdin);
     freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    for (int i = 0; i < n*2; i++)
    {int a;
        cin>>a;
        p.push_back(make_pair(a,i+1));
    }
    sort(p.begin(),p.end());
    int con=1,j=0;
    for(int i=0 ; i < (n*2)-1; i++)
    {
        if(p[i].first==p[i+1].first&&(con+1)%2==0)
        {
            cout<<p[i].second<<" "<<p[i+1].second<<endl;
            con+=2;
            i++;
            j=i;
        }
    }
    if(j+1<2*n)
        cout<<-1<<endl;
}


    #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <cmath>
#include<fstream>
using namespace std;
const int O=2e9;
const double E=1e-9;
const double pi = 3.1415926536;
int DX[]={1,-1,0,0};
int DY[]={0,0,1,-1};

        vector<pair<int,int> >p;

int main()
{
     freopen("input.txt","r",stdin);
     freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    for (int i = 0; i < n*2; i++)
    {int a;
        cin>>a;
        p.push_back(make_pair(a,i+1));
    }
    sort(p.begin(),p.end());
    for (int i = 0; i < (2*n)-1; i+=2)
    {
        if(p[i].first!=p[i+1].first)
        {
            cout<<-1<<endl;
            return 0;
        }
    }
    int con=1;
    for(int i=0 ; i < (n*2)-1; i++)
    {
        if(p[i].first==p[i+1].first&&(con+1)%2==0)
        {
            cout<<p[i].second<<" "<<p[i+1].second<<endl;
            con+=2;
            i++;

        }
    }
}


#include <iostream>
#include <string>
#include<math.h>
#include<vector>
#include<set>
#include <map>
#include<sstream>
#include<algorithm>
#include<ctime>
#include <cmath>
#include <stdlib.h>
#include<iomanip>
#include <climits>
using namespace std;
int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    int n;
    while (cin >> n)
    {
        int num;
        n *= 2;
        int  arr[5001];
        vector <pair<int,int> > v1(n);
        fill_n(arr, 5001, -1);
        int count = 0;
        for (int i = 0; i < n; i++)
        {
            cin >> num;
            if (arr[num]==-1)
            {
                arr[num] = i+1;
            }
            else
            {
                v1[count].first = i + 1;
                v1[count].second = arr[num];
                arr[num] = -1;
                count++;
            }

        }
        if (count!=n/2)
            cout << -1 << endl;
        else
        {
            for (int i = 0; i < count; i++)
            {
                cout << v1[i].first << " " << v1[i].second << endl;
            }
        }
    }

}


#include <iostream>
#include <string>
#include<math.h>
#include<vector>
#include<set>
#include <map>
#include<sstream>
#include<algorithm>
#include<ctime>
#include <cmath>
#include <stdlib.h>
#include<iomanip>
#include <climits>
using namespace std;
int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n;
    while (cin >> n)
    {
        int num;
        n *= 2;
        int  arr[5001];

        fill_n(arr, 5001, -1);
        bool l = 0;
        for (int i = 0; i < n; i++)
        {
            cin >> num;
            if (arr[num]==-1)
            {
                arr[num] = i+1;
            }
            else
            {
                cout << arr[num] << " " << i + 1 << endl;
                arr[num] = -1;
                l = 1;
            }

        }
        if (!l)
            cout << -1 << endl;
    }

}


#include <bits/stdc++.h>

using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef unsigned long long uint64;
typedef pair<int, int> pii;
typedef long long int64;
#define rep(i,n) for(int i = 0; i<(int)n; i++)
#define all(X) (X).begin(), (X).end()

struct info {
	int i, ind;
};
bool comp(const info &a, const info &b) {
	return a.i < b.i;
}
int main() {
#ifdef HOME
	freopen("in", "r", stdin);
#endif
	int n; int zor = 0;
	cin >> n;
	vector<info> v; info k;
	rep(i, 2*n) {
		cin >> k.i; zor ^= k.i; k.ind=  i+1;
		v.push_back(k);
	}
	if(zor) cout << -1;
	else {
		sort(all(v), comp);
		for(int i=0; i<v.size(); i+=2) {
			printf("%d %d\n", v[i].ind, v[i+1].ind);
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef unsigned long long uint64;
typedef pair<int, int> pii;
typedef long long int64;
#define rep(i,n) for(int i = 0; i<(int)n; i++)
#define all(X) (X).begin(), (X).end()

struct info {
	int i, ind;
};
bool comp(const info &a, const info &b) {
	return a.i < b.i;
}
int main() {
#ifdef HOME
	freopen("in", "r", stdin);
#endif
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	int n; int zor = 0;
	cin >> n; n<<=1;
	vector<info> v; info k;
	rep(i, n) {
		scanf("%d", &k.i); zor ^= k.i; k.ind=  i+1;
		v.push_back(k);
	}
	if(zor) cout << -1;
	else {
		sort(all(v), comp);
		for(int i=0; i<v.size(); i+=2) {
			printf("%d %d\n", v[i].ind, v[i+1].ind);
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef unsigned long long uint64;
typedef pair<int, int> pii;
typedef long long int64;
#define rep(i,n) for(int i = 0; i<(int)n; i++)
#define all(X) (X).begin(), (X).end()

struct info {
	int i, ind;
};
bool comp(const info &a, const info &b) {
	return a.i < b.i;
}
int main() {
#ifdef HOME
	freopen("in", "r", stdin);
#endif
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	int n;
	cin >> n; n<<=1;
	vector<info> v; info k;
	rep(i, n) {
		scanf("%d", &k.i); k.ind=  i+1;
		v.push_back(k);
	}
	sort(all(v), comp);
	for(int i=0; i<v.size(); i+=2) {
		if(v[i].i!=v [i+1].i) {
			cout << -1; return 0;
		}
	}
	for(int i=0; i<v.size(); i+=2) {
		printf("%d %d\n", v[i].ind, v[i+1].ind);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef unsigned long long uint64;
typedef pair<int, int> pii;
typedef long long int64;
#define rep(i,n) for(int i = 0; i<(int)n; i++)
#define all(X) (X).begin(), (X).end()

struct info {
	int i, ind;
};
bool comp(const info &a, const info &b) {
	return a.i < b.i;
}
int main() {
#ifdef HOME
	freopen("in", "r", stdin);
#endif
	int n; int zor = 0;
	cin >> n; n<<=1;
	vector<info> v; info k;
	rep(i, n) {
		scanf("%d", &k.i); zor ^= k.i; k.ind=  i+1;
		v.push_back(k);
	}
	if(zor) cout << -1;
	else {
		sort(all(v), comp);
		for(int i=0; i<v.size(); i+=2) {
			printf("%d %d\n", v[i].ind, v[i+1].ind);
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef unsigned long long uint64;
typedef pair<int, int> pii;
typedef long long int64;
#define rep(i,n) for(int i = 0; i<(int)n; i++)
#define all(X) (X).begin(), (X).end()

struct info {
	int i, ind;
};
bool comp(const info &a, const info &b) {
	return a.i < b.i;
}
int main() {
#ifdef HOME
	freopen("in", "r", stdin);
#endif
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n;
	cin >> n; n<<=1;
	vector<info> v; info k;
	rep(i, n) {
		scanf("%d", &k.i); k.ind=  i+1;
		v.push_back(k);
	}
	sort(all(v), comp);
	for(int i=0; i<v.size(); i+=2) {
		if(v[i].i!=v [i+1].i) {
			cout << -1; return 0;
		}
	}
	for(int i=0; i<v.size(); i+=2) {
		printf("%d %d\n", v[i].ind, v[i+1].ind);
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <limits>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <cstdlib>
#include <cstdio>
using namespace std;

int main(void)
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n;
    cin >> n;
    n *= 2;
    map<int, set<int> > q;
    for (int i = 0, v; i < n; ++i)
    {
        cin >> v;
        q[v].insert(i);
    }
    vector<int> ans;
    for (auto &x: q)
    {
        if (x.second.size() % 2 != 0)
        {
            cout << -1;
            return 0;
        }
        for (auto &y: x.second)
        {
            ans.push_back(y+1);
        }
    }
    for (int i = 0; i < n-1; i += 2)
    {
        cout << ans[i] << ' ' << ans[i+1] << endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0, _n = (int)(n); i < _n; i++)
#define fff(i, x, n) for (int i = (int)(x), _n = (int)(n); i < _n; i++)
#define ford(i, n, x) for (int i = (int)(n), _x = (int)(x); i-- > _x; )
#define fmp(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define len(x) (int((x).size()))
#define pb push_back
#define fx(x) fixed<<setprecision(x)
#define mkp make_pair
#define all(X) (X).begin(),(X).end()
#define see(x) cerr << #x << "= " << x << endl
#define reset(a, b) memset(a, b, sizeof(a))

#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> pii;

template <class T> inline void smin(T &a, T b){ if(b<a) a = b;}
template <class T> inline void smax(T &a, T b){ if(a<b) a = b;}
///////////////////////////////////////////////////////////////

const int NN = 3 * 100005;

vector<pii> a;

int main() {
    ios::sync_with_stdio(false);
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n; cin >> n;
    a.resize(n * 2);
    rep(i, 2*n) {
        cin >> a[i].fi ;
        a[i].se = i+1;
    }
    sort(all(a));
    vector <int> sum;
    bool f = true;
    int i=0 , j = 2 * n - 1;
    while(i<j) {
        sum.pb(a[i].fi + a[j].fi);
        i ++ ; j--;
    }
    sort(all(sum));
    if(sum[0] != sum[n-1]) f = false;
    if(n == 1 && a[0].fi != a[1].fi) f = false;
    if(!f) {
        cout << -1 << endl;
    } else {
        int i=0, j=(2*n)-1;
        while(i<j) {
            cout << a[i].se << ' ' << a[j].se << endl;
            i++ ; j--;
        }
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0, _n = (int)(n); i < _n; i++)
#define fff(i, x, n) for (int i = (int)(x), _n = (int)(n); i < _n; i++)
#define ford(i, n, x) for (int i = (int)(n), _x = (int)(x); i-- > _x; )
#define fmp(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define len(x) (int((x).size()))
#define pb push_back
#define fx(x) fixed<<setprecision(x)
#define mkp make_pair
#define all(X) (X).begin(),(X).end()
#define see(x) cerr << #x << "= " << x << endl
#define reset(a, b) memset(a, b, sizeof(a))

#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> pii;

template <class T> inline void smin(T &a, T b){ if(b<a) a = b;}
template <class T> inline void smax(T &a, T b){ if(a<b) a = b;}
///////////////////////////////////////////////////////////////

vector<pii> a;

int main() {
    ios::sync_with_stdio(false);
    int n; cin >> n;
    a.resize(n * 2);
    rep(i, 2*n) {
        cin >> a[i].fi ;
        a[i].se = i+1;
    }
    sort(all(a));
    vector <int> sum;
    bool f = true;
    int i=0 , j = 2 * n - 1;
    while(i<j) {
        sum.pb(a[i].fi + a[j].fi);
        i ++ ; j--;
    }
    sort(all(sum));
    if(sum[0] != sum[n-1]) f = false;
    if(n == 1 && a[0].fi != a[1].fi) f = false;
    if(!f) {
        cout << -1 << endl;
    } else {
        int i=0, j=(2*n)-1;
        while(i<j) {
            cout << a[i].se << ' ' << a[j].se << endl;
            i++ ; j--;
        }
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0, _n = (int)(n); i < _n; i++)
#define fff(i, x, n) for (int i = (int)(x), _n = (int)(n); i < _n; i++)
#define ford(i, n, x) for (int i = (int)(n), _x = (int)(x); i-- > _x; )
#define fmp(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define len(x) (int((x).size()))
#define pb push_back
#define fx(x) fixed<<setprecision(x)
#define mkp make_pair
#define all(X) (X).begin(),(X).end()
#define see(x) cerr << #x << "= " << x << endl
#define reset(a, b) memset(a, b, sizeof(a))

#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> pii;

template <class T> inline void smin(T &a, T b){ if(b<a) a = b;}
template <class T> inline void smax(T &a, T b){ if(a<b) a = b;}
///////////////////////////////////////////////////////////////

const int NN = 3 * 100005;

vector<int> a[5555];
int mapp[5555];
int main() {
    //ios::sync_with_stdio(false);
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n; scanf("%d",&n);

    rep(i, 2*n) {
        int x; scanf("%d",&x);
        a[x].pb(i+1);
        mapp[x] ++ ;
    }
    bool f = true;
    fff(i, 1, 5001) {
        if(mapp[i] & 1) {
            f = false ; break;
        }
    }
    if(!f) {
        cout << -1 << endl;
    } else {
        fff(i, 1, 5001) {
            for(int j=0 ; j<len(a[i]) ; j+=2) {
                printf("%d %d\n",a[i][j], a[i][j+1]);
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0, _n = (int)(n); i < _n; i++)
#define fff(i, x, n) for (int i = (int)(x), _n = (int)(n); i < _n; i++)
#define ford(i, n, x) for (int i = (int)(n), _x = (int)(x); i-- > _x; )
#define fmp(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define len(x) (int((x).size()))
#define pb push_back
#define fx(x) fixed<<setprecision(x)
#define mkp make_pair
#define all(X) (X).begin(),(X).end()
#define see(x) cerr << #x << "= " << x << endl
#define reset(a, b) memset(a, b, sizeof(a))

#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> pii;

template <class T> inline void smin(T &a, T b){ if(b<a) a = b;}
template <class T> inline void smax(T &a, T b){ if(a<b) a = b;}
///////////////////////////////////////////////////////////////

const int NN = 3 * 100005;

vector<pii> a;
int mapp[5555];
int main() {
    //ios::sync_with_stdio(false);
  freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n; scanf("%d",&n);
    a.resize(n * 2);
    rep(i, 2*n) {
        cin >> a[i].fi ;
        a[i].se = i+1;
        mapp[a[i].fi] ++ ;
    }
    bool f = true;
    fff(i, 1, 5001) {
        if(mapp[i] & 1) {
            f = false ; break;
        }
    }
    if(!f) {
        cout << -1 << endl;
    } else {
        sort(all(a));
        for(int i=0 ; i<2*n ; i+=2) {
            //cout << a[i].se << ' ' << a[i+1].se << endl;
            printf("%d %d\n",a[i].se, a[i+1].se);
        }
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0, _n = (int)(n); i < _n; i++)
#define fff(i, x, n) for (int i = (int)(x), _n = (int)(n); i < _n; i++)
#define ford(i, n, x) for (int i = (int)(n), _x = (int)(x); i-- > _x; )
#define fmp(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define len(x) (int((x).size()))
#define pb push_back
#define fx(x) fixed<<setprecision(x)
#define mkp make_pair
#define all(X) (X).begin(),(X).end()
#define see(x) cerr << #x << "= " << x << endl
#define reset(a, b) memset(a, b, sizeof(a))

#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> pii;

template <class T> inline void smin(T &a, T b){ if(b<a) a = b;}
template <class T> inline void smax(T &a, T b){ if(a<b) a = b;}
///////////////////////////////////////////////////////////////

const int NN = 3 * 100005;

vector<pii> a;
map<int,int> mapp;
int main() {
    ios::sync_with_stdio(false);
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n; cin >> n;
    a.resize(n * 2);
    rep(i, 2*n) {
        cin >> a[i].fi ;
        a[i].se = i+1;
        mapp[a[i].fi] ++ ;
    }
    bool f = true;
    fmp(it, mapp) {
        if(it->se & 1) {
            f = false; break;
        }
    }
    if(!f) {
        cout << -1 << endl;
    } else {
        sort(all(a));
        for(int i=0 ; i<2*n ; i+=2) {
            cout << a[i].se << ' ' << a[i+1].se << endl;
        }
    }
    return 0;
}


//4304346	 Aug 17, 2013 7:38:25 PM	fuwutu	 254A - Cards with Numbers	 GNU C++0x	Accepted	530 ms	4800 KB
#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    int n;
    scanf("%d", &n);

    pair<int, int>* x = new pair<int, int>[n * 2];
    for (int i = 0; i < n * 2; ++i)
    {
        scanf("%d", &x[i].first);
        x[i].second = i + 1;
    }

    sort(x, x + n * 2);
    bool possible = true;
    for (int i = 0; i < n * 2; i += 2)
    {
        if (x[i].first != x[i+1].first)
        {
            possible = false;
            break;
        }
    }

    if (possible)
    {
        for (int i = 0; i < n * 2; i += 2)
        {
            printf("%d %d\n", x[i].second, x[i+1].second);
        }
    }
    else
    {
        printf("-1\n");
    }

    delete[] x;
    return 0;
}


#include <cassert>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
#include <complex>
using namespace std;

// begin insert defines
typedef pair<int, int> PII;
#define ALL(a) (a).begin(),(a).end()
#define Rep(i,n) for(int n_ = (n), i = 0; i< n_; ++i)
#define ND second
#define FS first
#define MP make_pair
#define PB push_back
#define VR vector

// end insert defines

int main(int argc, char *argv[])
{
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  int n;
  cin >> n;
  VR<PII> a;
  Rep(i, n * 2) {
    int v;
    cin >> v;
    a.PB(MP(v, i));
  }
  sort(ALL(a));

  bool lab = true;
  Rep(i, n) {
    if (a[i * 2].FS != a[i * 2 + 1].FS) {
      lab = false;
      break;
    }
  }
  if (!lab) cout << -1 << endl;
  else {
    Rep(i, n)
      cout << a[i * 2].ND + 1 << " " << a[i * 2 + 1].ND + 1 << endl;
  }
  return 0;
}


#include <cassert>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
#include <complex>
using namespace std;

// begin insert defines
typedef pair<int, int> PII;
#define ALL(a) (a).begin(),(a).end()
#define Rep(i,n) for(int n_ = (n), i = 0; i< n_; ++i)
#define ND second
#define FS first
#define MP make_pair
#define PB push_back
#define VR vector

// end insert defines

int main(int argc, char *argv[])
{
  int n;
  cin >> n;
  VR<PII> a;
  Rep(i, n * 2) {
    int v;
    cin >> v;
    a.PB(MP(v, i));
  }
  sort(ALL(a));

  bool lab = true;
  Rep(i, n) {
    if (a[i * 2].FS != a[i * 2 + 1].FS) {
      lab = false;
      break;
    }
  }
  if (!lab) cout << -1 << endl;
  else {
    Rep(i, n)
      cout << a[i * 2].ND + 1 << " " << a[i * 2 + 1].ND + 1 << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    int n, t;
    cin >> n;
    unordered_map<int, vector<int>> ind;
    for(int i = 1; i <= 2* n; i++)
        cin >> t, ind[t].push_back(i);

    for(auto i : ind)
        if(i.second.size() % 2 != 0){
            cout << -1;
            return 0;
        }

    for(auto i : ind){
        for(int j = 0; j < i.second.size(); j += 2)
            cout << i.second[j] << " " << i.second[j + 1] << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios_base::sync_with_stdio(false);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    int n, t;
    cin >> n;
    vector<vector<int>> ind(5010, vector<int>());
    for(int i = 1; i <= 2* n; i++)
        cin >> t, ind[t].push_back(i);

    for(auto i : ind)
        if(i.size() % 2 != 0){
            cout << -1;
            return 0;
        }

    for(auto i : ind)
        for(int j = 0; j < i.size(); j += 2)
            cout << i[j] << " " << i[j + 1] << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    int n, t;
    cin >> n;
    unordered_map<int, vector<int>> ind;
    for(int i = 1; i <= 2* n; i++)
        cin >> t, ind[t].push_back(i);

    for(auto i : ind)
        if(i.second.size() % 2 != 0){
            cout << -1;
            return 0;
        }

    for(auto i : ind){
        for(int j = 0; j < i.second.size(); j += 2)
            cout << i.second[j] << " " << i.second[j + 1] << "\n";
    }
    return 0;
}


#define _USE_MATH_DEFINES
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <map>
#include <cstdio>
#include <stack>
#include <iostream>
#include <queue>
#include <climits>
#include <bitset>
#include <set>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <deque>
#include <iomanip>
#include <list>
#include <cmath>

using namespace std;
#define rep(i, n) for(int i=0; i<(int)n; i++)
#define rrep (i,n) for(int i=(int)n-1; i>=0; i--)
#define foreach(c,it) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define int64 long long
#define uint64 unsigned int64
#define mp make_pair

template <typename type, typename d = char>
void printArray(type *A, int n, d delim = ' ') { rep(i,n) cout << fixed <<  A[i] << delim; }
template <typename type>
type *scanArray(int n) { type *A = new type[n]; rep(i,n) cin >> A[i]; return A; }
template <typename type>
type *genArray(int n, type init) { type *A = new type[n]; rep(i,n) A[i]=init; return A; }

int daysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int days(int month, bool isLeap = false) {
    if (month == 1 && isLeap) return 29;
    else return daysInMonth[month];
}

int main (int argc, const char * argv[])
{
    cout.precision(9);
#ifdef MISTMAC
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif
    //--------------------------------------
    int n;
    scanf("%d", &n);
    vector< pair<int, int> > A(2*n);
    rep(i,2*n) {
        int a;
        scanf("%d", &a);
        A[i] = mp(a,i);
    }
    sort(A.begin(), A.end());
    vector< pair<int, int> > ans;
    rep(i,2*n) {
        if (A[i].first!=A[i+1].first) {
            printf("-1");
            return 0;
        } else {
            ans.push_back(mp(A[i].second, A[i+1].second));
        }
        i++;
    }
    rep(i,n) {
        printf("%d %d\n", ans[i].first+1, ans[i].second+1);
    }
    //--------------------------------------
#ifdef MISTMAC
	fclose(stdout);
	fclose(stdin);
#endif
    return 0;
}


#define MISTMAC
#define _USE_MATH_DEFINES
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <map>
#include <cstdio>
#include <stack>
#include <iostream>
#include <queue>
#include <climits>
#include <bitset>
#include <set>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <deque>
#include <iomanip>
#include <list>
#include <cmath>

using namespace std;
#define rep(i, n) for(int i=0; i<(int)n; i++)
#define rrep (i,n) for(int i=(int)n-1; i>=0; i--)
#define foreach(c,it) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define int64 long long
#define uint64 unsigned int64
#define mp make_pair

template <typename type, typename d = char>
void printArray(type *A, int n, d delim = ' ') { rep(i,n) cout << fixed <<  A[i] << delim; }
template <typename type>
type *scanArray(int n) { type *A = new type[n]; rep(i,n) cin >> A[i]; return A; }
template <typename type>
type *genArray(int n, type init) { type *A = new type[n]; rep(i,n) A[i]=init; return A; }

int daysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int days(int month, bool isLeap = false) {
    if (month == 1 && isLeap) return 29;
    else return daysInMonth[month];
}

int main (int argc, const char * argv[])
{
    cout.precision(9);
#ifdef MISTMAC
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif
    //--------------------------------------
    int n;
    scanf("%d", &n);
    vector< pair<int, int> > A(2*n);
    rep(i,2*n) {
        int a;
        scanf("%d", &a);
        A[i] = mp(a,i);
    }
    sort(A.begin(), A.end());
    vector< pair<int, int> > ans;
    rep(i,2*n) {
        if (A[i].first!=A[i+1].first) {
            printf("-1");
            return 0;
        } else {
            ans.push_back(mp(A[i].second, A[i+1].second));
        }
        i++;
    }
    rep(i,n) {
        printf("%d %d\n", ans[i].first+1, ans[i].second+1);
    }
    //--------------------------------------
#ifdef MISTMAC
	fclose(stdout);
	fclose(stdin);
#endif
    return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<cmath>

#define nx(x) (x | (x+1))
#define pr(x) (x & (x+1))

#define fi first
#define MAXN 200005
#define se second
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define mem(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long int64;

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int i,j,n;
    vector<int> m[5001];
    cin>>n;

    for(i=1;i<=n+n;i++)
      {
          cin>>j;
          m[j].pb(i);
      }
      int ans=0;
    for(i=1;i<=5000;i++)
    {
        if(m[i].size()>1)ans++;
    }
    if(ans!=n)
    {
        cout<<-1;return 0;
    }
    for(i=1;i<=5000;i++)
    {
        if(m[i].size()>1)
        {
            cout<<m[i][0]<<' '<<m[i][1]<<endl;
        }
    }

return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<cmath>

#define nx(x) (x | (x+1))
#define pr(x) (x & (x+1))

#define fi first
#define MAXN 200005
#define se second
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define mem(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long int64;

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int i,j,n;
    map<int,vector<int> > m;
    cin>>n;
    for(i=1;i<=n+n;i++)
    {
        cin>>j;
        m[j].pb(i);
    }
    int ans=0;
    tr(m,it)
    {
        if((*it).second.size()>1)
        {
            ans++;
            //cout<<(*it)[0]<<' '<<(*it)[1]<<endl;
        }
    }
    if(ans!=n){cout<<-1;;return 0;}
    tr(m,it)
    {
        if((*it).second.size()>1)
        {
            //ans++;
            cout<<(*it).second[0]<<' '<<(*it).second[1]<<endl;
        }
    }

return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<sstream>
#include<cmath>

#define nx(x) (x | (x+1))
#define pr(x) (x & (x+1))

#define fi first
#define MAXN 200005
#define se second
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define mem(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long int64;

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int i,j,n;
    vector<int> m[5001];
    cin>>n;

    for(i=1;i<=n+n;i++)
      {
          cin>>j;
          m[j].pb(i);
      }
      int ans=0;
    for(i=1;i<=5000;i++)
    {
        if(m[i].size()>1)ans+=m[i].size()/2;
    }
    if(ans!=n)
    {
        cout<<-1;return 0;
    }
    for(i=1;i<=5000;i++)
    {
        if(m[i].size()>1)
        {
            for(j=1;j<m[i].size();j+=2)
              cout<<m[i][j-1]<<' '<<m[i][j]<<endl;

        }
    }

return 0;
}


#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <cstdio>
using namespace std;

#define pb push_back
#define mp make_pair
#define X first
#define Y second

vector<pair<int, int>> x;
map<int, int> f;
int n, cnt;

int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin >> n;
	for (int i = 1, A; i <= n + n; ++i)
	{
		cin >> A;
		if (f[A] == 0) f[A] = i, ++cnt;
		else x.pb(mp(f[A], i)), f[A] = 0, --cnt;
	}
	if (cnt) cout << -1 << endl;
	else
		for (auto i : x)
			cout << i.X << ' ' << i.Y << endl;
	return 0;
}


#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;

#define pb push_back
#define mp make_pair
#define X first
#define Y second

vector<pair<int, int>> x;
map<int, int> f;
int n, cnt;

int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 1, A; i <= n + n; ++i)
	{
		cin >> A;
		if (f[A] == 0) f[A] = i, ++cnt;
		else x.pb(mp(f[A], i)), f[A] = 0, --cnt;
	}
	if (cnt) cout << -1 << endl;
	else
		for (auto i : x)
			cout << i.X << ' ' << i.Y << endl;
	return 0;
}


#include<fstream>
#include<utility>
#include<vector>
#include<algorithm>

using namespace std;

bool sort_cb (pair<int, int> a, pair<int, int> b) {
    return a.second > b.second;
}

int main () {
   ifstream fin("input.txt");
   ofstream fout("output.txt");

   int n; fin >> n;

   vector< pair<int, int> > v;

   for (int i=0 ; i<2*n ; i++) {
       int x;
       fin >> x;
       v.push_back(make_pair(i, x));
   }

   bool solved = true;

   for (int i=0 ; i<n ; i++) {
       if (v[i*2].second != v[i*2+1].second) {
           fout << "-1";
           solved = false;
       }
   }

   if (solved) {
       for (int i=0 ; i<n ; i++) {
           fout << v[i*2].first << v[i*2+1].first << endl;
       }
   }

   sort(v.begin(), v.end(), sort_cb);
   fin.close();
   fout.close();
}


#include<fstream>
#include<utility>
#include<vector>
#include<algorithm>

using namespace std;

bool sort_cb (pair<int, int> a, pair<int, int> b) {
    return a.second > b.second;
}

int main () {
   ifstream cin("input.txt");
   ofstream cout("output.txt");

   int n; cin >> n;

   vector< pair<int, int> > v;

   for (int i=0 ; i<2*n ; i++) {
       int x;
       cin >> x;
       v.push_back(make_pair(i, x));
   }

   sort(v.begin(), v.end(), sort_cb);
   bool solved = true;

   for (int i=0 ; i<n ; i++) {
       if (v[i*2].second != v[i*2+1].second) {
           solved = false;
           break;
       }
   }

   if (solved) {
       for (int i=0 ; i<n ; i++) {
           cout << v[i*2].first+1 << " " << v[i*2+1].first+1 << endl;
       }
   } else {
       cout << "-1";
   }
   cin.close();
   cout.close();
}


#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>

using namespace std;

bool sort_cb (pair<int, int> a, pair<int, int> b) {
    return a.second > b.second;
}

int main () {
   ifstream fin("input.txt");
   ofstream fout("output.txt");

   int n; fin >> n;

   vector< pair<int, int> > v;

   for (int i=0 ; i<2*n ; i++) {
       int x;
       fin >> x;
       v.push_back(make_pair(i, x);
   }

   bool solved = true;

   for (int i=0 ; i<n ; i++) {
       if (v[i*2].second != v[i*2+1].second) {
           fout << "-1";
           solved = false;
       }
   }

   if (solved) {
       for (int i=0 ; i<n ; i++) {
           fout << v[i*2].first << v[i*2+1].first << endl;
       }
   }

   sort(v.begin(), v.end(), sort_cb);
   fin.close();
   fout.close();
}


#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>

using namespace std;

bool sort_cb (pair<int, int> a, pair<int, int> b) {
    return a.second() > b.second();
}

int main () {
   ifstream fin("input.txt");
   ofstream fout("output.txt");

   int n; fin >> n;

   vector< pair<int, int> > v;

   for (int i=0 ; i<2*n ; i++) {
       int x;
       fin >> x;
       v.push_back(make_pair(i, x);
   }

   bool solved = true;

   for (int i=0 ; i<n ; i++) {
       if (v[i*2].second() != v[i*2+1].second()) {
           fout << "-1";
           solved = false;
       }
   }

   if (solved) {
       for (int i=0 ; i<n ; i++) {
           fout << v[i*2].first() << v[i*2+1].first() << endl;
       }
   }

   sort(v.begin(), v.end(), sort_cb);
   fin.close();
   fout.close();
}


#include<fstream>
#include<utility>
#include<vector>
#include<algorithm>
#include<iostream>

using namespace std;

bool sort_cb (pair<int, int> a, pair<int, int> b) {
    return a.second > b.second;
}

int main () {
   ifstream fin("input.txt");
   ofstream fout("output.txt");

   int n; cin >> n;

   vector< pair<int, int> > v;

   for (int i=0 ; i<2*n ; i++) {
       int x;
       cin >> x;
       v.push_back(make_pair(i, x));
   }

   sort(v.begin(), v.end(), sort_cb);
   bool solved = true;

   for (int i=0 ; i<n ; i++) {
       if (v[i*2].second != v[i*2+1].second) {
           solved = false;
           break;
       }
   }

   if (solved) {
       for (int i=0 ; i<n ; i++) {
           cout << v[i*2].first+1 << " " << v[i*2+1].first+1 << endl;
       }
   } else {
       cout << "-1";
   }
   fin.close();
   fout.close();
}


#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>

using namespace std;

bool sort_cb (pair<int, int> a, pair<int, int> b) {
    return a.second > b.second;
}

int main () {
   ifstream fin("input.txt");
   ofstream fout("output.txt");

   int n; fin >> n;

   vector< pair<int, int> > v;

   for (int i=0 ; i<2*n ; i++) {
       int x;
       fin >> x;
       v.push_back(make_pair(i, x));
   }

   bool solved = true;

   for (int i=0 ; i<n ; i++) {
       if (v[i*2].second != v[i*2+1].second) {
           fout << "-1";
           solved = false;
       }
   }

   if (solved) {
       for (int i=0 ; i<n ; i++) {
           fout << v[i*2].first << v[i*2+1].first << endl;
       }
   }

   sort(v.begin(), v.end(), sort_cb);
   fin.close();
   fout.close();
}