#include <cstdio>
#include <vector>
#include <algorithm>

#define MOD (1000000007)
#define EPS (1e-9)
#define INF (2117117117)
#define LLINF (2117117117117117117LL)
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define sqr(a) ((a) * (a))
#define sz(a) ((int) (a).size())

typedef unsigned int uint;
typedef long long llong;
typedef long double ldouble;
typedef unsigned long long ullong;

using namespace std;

#include "grader.h"

int identify_set(int n, int k, llong **sets)
{
    if (k == 1)
        return 0;
    if (n == 1)
        return -1;
    int ans = -1, cntans = 0, m = (n + 1) / 2;
    llong pw1 = 1, pw2 = 1;
    for (int i = 0; i < m; i++)
        pw1 *= 3;
    for (int i = 0; i < n - m; i++)
        pw2 *= 3;
    vector<pair<pair<llong, llong>, pair<int, int> > > a(pw1), b(pw2);
    for (int cur = 0; cur < k; cur++)
    {
        if (ans == -1)
        {
            ans = cur;
            cntans = 1;
            continue;
        }
        int cnt = 0;
        for (int i = 0; i < (1 << m); i++)
        {
            for (int j = i; j; j = ((j - 1) & i))
            {
                llong sum1 = 0, sum2 = 0;
                for (int t = 0; t < m; t++)
                {
                    if ((j >> t) & 1)
                    {
                        sum1 += sets[ans][t];
                        sum2 += sets[cur][t];
                    }
                    else if ((i >> t) & 1)
                    {
                        sum1 -= sets[ans][t];
                        sum2 -= sets[cur][t];
                    }
                }
                a[cnt++] = mp(mp(sum1, -sum2), mp(j, i ^ j));
            }
            llong sum1 = 0, sum2 = 0;
            for (int t = 0; t < m; t++)
                if ((i >> t) & 1)
                {
                    sum1 -= sets[ans][t];
                    sum2 -= sets[cur][t];
                }
            a[cnt++] = mp(mp(sum1, -sum2), mp(0, i));
        }
        cnt = 0;
        for (int i = 0; i < (1 << (n - m)); i++)
        {
            for (int j = i; j; j = ((j - 1) & i))
            {
                llong sum1 = 0, sum2 = 0;
                for (int t = 0; t < (n - m); t++)
                {
                    if ((j >> t) & 1)
                    {
                        sum1 += sets[ans][t + m];
                        sum2 += sets[cur][t + m];
                    }
                    else if ((i >> t) & 1)
                    {
                        sum1 -= sets[ans][t + m];
                        sum2 -= sets[cur][t + m];
                    }
                }
                b[cnt++] = mp(mp(-sum1, sum2), mp(j, i ^ j));
            }
            llong sum1 = 0, sum2 = 0;
            for (int t = 0; t < (n - m); t++)
                if ((i >> t) & 1)
                {
                    sum1 -= sets[ans][t + m];
                    sum2 -= sets[cur][t + m];
                }
            b[cnt++] = mp(mp(-sum1, sum2), mp(0, i));
        }
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        llong mx = -LLINF;
        int i, j, mxi;
        for (i = j = 0, mxi = -1; i < sz(a); i++)
        {
            for (; j < sz(b) && b[j].first < a[i].first; j++)
                if (mx < -b[j].first.second)
                {
                    mx = -b[j].first.second;
                    mxi = j;
                }
            if (mx >= -a[i].first.second && a[i].first != b[mxi].first)
                break;
        }
        if (i == sz(a))
        {
            cntans++;
            continue;
        }
        int res = compare(a[i].second.first | (b[mxi].second.first << m),
                a[i].second.second | (b[mxi].second.second << m)),
            res1 = (a[i].first.first + b[mxi].first.first == 0 ? 0 : 1),
            res2 = (a[i].first.second - b[mxi].first.second == 0 ? 0 : -1);
        if (res2 == res)
        {
            ans = cur;
            cntans = 1;
        }
        else if (res1 != res)
        {
            ans = -1;
            cntans = 0;
        }
    }
    return (cntans == 1 ? ans : -1);
}
