#include "bits/stdc++.h"
#include "grader.h"
#define puba push_back
#define mapa make_pair
#define ff first
#define ss second
#define bend(_x) (_x).begin(), (_x).end()
#define szof(_x) ((int) (_x).size())
#define TASK_NAME ""

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 1 << 20, MAXK = 26;

vector<pair<ll, int>> w[MAXK];
vector<int> where[MAXK];

int sign(int a, int b) {
    if (a < b) {
        return -1;
    }
    if (a == b) {
        return 0;
    }
    return 1;
}

int identify_set(int n, int k, long long **sets) {
    int bdv = 1 << n;
    for (int i = 0; i < k; ++i) {
        w[i] = vector<pair<ll, int>>(bdv);
        where[i] = vector<int>(bdv);
        //reverse(sets[i], sets[i] + n);
    }
    for (int i = 1; i < bdv; ++i) {
        int bit = ((i ^ (i - 1)) + 1) / 2;
        int ind = 0;
        int tmp = bit;
        while (tmp != 1) {
            ++ind;
            tmp /= 2;
        }

        for (int j = 0; j < k; ++j) {
            w[j][i] = {w[j][i ^ bit].ff + sets[j][ind], i};
        }
    }

    /*
    for (int i = 0; i < k; ++i) {
        for (int j = 0; j < bdv; ++j) {
            cerr << w[i][j].ff << " ";
        }
        cerr << endl;
    }*/

    for (int i = 0; i < k; ++i) {
        sort(bend(w[i]));
        for (int j = 0; j < bdv; ++j) {
            where[i][w[i][j].ss] = j;
            //cerr << w[i][j].ss << " ";
        }
        //cerr << endl;
    }
    int iam = 0;
    bool bad_ans = false;
    for (int i = 1; i < k; ++i) {
        //cerr << "i am " << iam << endl;
        ll biggest = -1;
        int mask1 = -1;
        int mask2 = -1;
        for (int j = 0; j < bdv; ++j) {
            int q = w[iam][j].ss;
            ll nw = w[i][where[i][q]].ff;
            if (nw < biggest || (j > 0 && nw == biggest && w[iam][j].ff > w[iam][where[iam][mask1]].ff)) {
                //cerr << nw << " " << biggest << endl;
                mask2 = q;
                break;
            } else {
                mask1 = q;
                biggest = nw;
            }
        }
        if (mask2 == -1) {
            //cerr << "set bad ans" << endl;
            bad_ans = true;
            continue;
        }
        //cerr << mask1 << " " << mask2 << endl;
        int tmp = mask1 & mask2;
        int res = compare(mask1 ^ tmp, mask2 ^ tmp);
        //cerr << "res: " << res << endl;
        //cerr << w[iam][where[iam][mask1]].ff << " " << w[iam][where[iam][mask2]].ff << endl;
        //cerr << sign(w[iam][where[iam][mask1]].ff, w[iam][where[iam][mask2]].ff) << endl;
        if (res != sign(w[iam][where[iam][mask1]].ff, w[iam][where[iam][mask2]].ff)) {
            bad_ans = false;
            iam = i;
        }
    }
    //cerr << "ans: " << iam << endl;
    if (bad_ans) {
        return -1;
    }
    return iam;
} 