#include <bits/stdc++.h>
#include "code.h"
#define maxn 1010
using namespace std;

int a[maxn][maxn], b[maxn][maxn], minC[maxn], maxR[maxn], row[maxn], col[maxn];

vector<int> Nadji(int n, int k){
    if(k == n*n){
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n; ++j){
                b[i][j] = Pitaj(i, j);
            }
        }
        for(int i = 1; i <= n; ++i){
            minC[i] = b[1][i];
            maxR[i] = b[i][1];
            for(int j = 1; j <= n; ++j){
                minC[i] = min(minC[i], b[j][i]);
                maxR[i] = max(maxR[i], b[i][j]);
            }
        }
        for(int i = 1; i <= n; ++i){
            int a1 = 0, a2 = 0;
            for(int j = 1; j <= n ;++j){
                if(maxR[i] == b[i][j]){
                    ++a1;
                }
                if(minC[i] == b[j][i]){
                    ++a2;
                }
            }
            if(a1 > 1){
                maxR[i] = -1e9;
            }
            if(a2 > 1){
                minC[i] = 1e9+2;
            }
        }
        for(int i = 1; i <= n; ++i)
        {
            for(int j = 1; j <= n; ++j){
                cerr << b[i][j] << ' ';
            }
        }
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n; ++j){
                if((b[i][j] == minC[j]) and (b[i][j] == maxR[i])){
                    return {i, j};
                }
            }
        }
        return {0, 0};
    }
    if(n == 1000 and k == 5000){
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n; ++j){
                if(!row[i] and !col[j]){
                    int tmp = Pitaj(i, j);
                    if(tmp == 1){
                        col[j] = 1;
                    } else if(tmp == 3){
                        row[i] = 1;
                    } else {
                        for(int k = 1; k <= n; ++k){
                            if(k == j){
                                continue;
                            }
                            int t = Pitaj(i, k);
                            if(t == 2){
                                row[i] = 1;
                                break;
                            } else if(t == 3){
                                row[i] = 1;
                                break;
                            } else {
                                col[k] = 1;
                            }
                        }
                        if(!row[i]){
                            for(int k = 1; k <= n; ++k){
                                if(k == i){
                                    continue;
                                }
                                int t = Pitaj(k, j);
                                if(t == 2){
                                    col[j] = 1;
                                    break;
                                } else if(t == 1){
                                    col[j] = 1;
                                    break;
                                } else {
                                    row[k] = 1;
                                }
                            }
                            if(!col[j]){
                                return {i, j};
                            }
                        }
                    }
                }
            }
        }
        return {0, 0};
    }
    return {0, 0};
}