#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
#include<string>
#include<algorithm>
#include<set>
#include<vector>
#define ll long long
#include<ctime>
#include<random>
#define ff first
#define ss second
using namespace std;
#include "code.h"
const int maxn=1e5;



int n,k;


typedef pair<int,int> point;
map<point,int>mapa;
set<point>pos;
///int n;
point rez,glob;
int qnum,col[maxn+10],row[maxn+10];
bool ende(){
   if(rez!=(point){0,0})return 1;
   return 0;
}
point get_point(){
    mt19937 gen(time(NULL));
    while(1){
        point pom={gen()%n+1,gen()%n+1};
        if(mapa.find(pom)==mapa.end())return pom;
    }
}
int uzmi(point x){
   if(x.ff<1 || x.ff>n || x.ss<1 || x.ss>n)return -1;

   if(mapa.find(x)!=mapa.end())return mapa[x];
   else{
    qnum++;
    int pom=Pitaj(x.ff,x.ss);
    mapa[x]=pom;/// ubaci
    return pom;
   }
}
void update(point x,int pom){
    if(pom==-1)return;
    col[x.ss]=min(pom,col[x.ss]);/// update col
    row[x.ff]=max(pom,row[x.ff]);/// update row
}
bool check_row(point x,int val){
   if(x.ff<1 || x.ff>n || x.ss<1 || x.ss>n)return 0;

   if(row[x.ff]>=val)return false;
   return true;
}
bool check_col(point x,int val){
   if(x.ff<1 || x.ff>n || x.ss<1 || x.ss>n)return 0;

   if(col[x.ss]<=val)return false;
   return true;
}

point try_finish(point p,int val){

    ///printf("%d %d  tryfinish\n",p.ff,p.ss);

   for(int i=1;i<=n;i++){///  go col
      int curr=uzmi({i,p.ss});
      if(i==p.ff)continue;

      if(val>=curr){
        glob={i,p.ss};
        return {0,0};
      }
      update({i,p.ss},curr);
   }

   for(int i=1;i<=n;i++){///  go row
      int curr=uzmi({p.ff,i});
      if(i==p.ss)continue;

      if(val<=curr){
        glob={p.ff,i};
        return {0,0};
      }
      update({p.ff,i},curr);
   }

   ///printf("finished \n");
   return p;
}


void go(point p){

   if(pos.find(p)!=pos.end())return;
   pos.insert(p);

   ///printf("%d %d  sada\n",p.ff,p.ss);

   int curr=uzmi(p);
//   if(qnum==k){
//    rez={0,0};
//    return;
//   }


   /// ako nema smisla da ovaj bude kandidat(check u row i col)

   if(!check_col(p,curr)){
      update(p,curr);
      return;
   }
   if(!check_row(p,curr)){
      update(p,curr);
      return;
   }

   update(p,curr);

   ///printf("%d %d  prosao\n",p.ff,p.ss);

   int e=1;


   /// go col
   int pom1=uzmi({p.ff+1,p.ss});
   go({p.ff+1,p.ss});
   if(ende())return;

   int pom2=uzmi({p.ff-1,p.ss});
   go({p.ff-1,p.ss});
   if(ende())return;

   if( (pom1<=curr && pom1!=-1) || (pom2<=curr && pom2!=-1))e=0;



   /// go row
   pom1=uzmi({p.ff,p.ss+1});
   go({p.ff,p.ss+1});
   if(ende())return;

   pom2=uzmi({p.ff,p.ss-1});
   go({p.ff,p.ss-1});
   if(ende())return;

   if( (pom1>=curr && pom1!=-1) || (pom2>=curr && pom2!=-1) )e=0;

   ///printf("%d %d   %d %d %d %d  gee\n",p.ff,p.ss,e,pom1,pom2,curr);


   if(e==1){
     glob={0,0};
     rez=try_finish(p,curr);
     if(ende())return;

     go(glob);
   }

}
std::vector<int> Nadji(int N, int K)
{
    n=N;
    rez={0,0};
    for(int i=1;i<=n;i++){
        row[i]=0;
        col[i]=1e9+5;
    }
    mt19937 gen(time(NULL));

    while(qnum<k && mapa.size()<n*n){
      point start=get_point();
      ///printf("  NEW POINT !!!!!!\n");
      ///start={4,4};
      go(start);
      if(ende())break;
    }

    if(rez=={0,0})while(1){}

	// primer kada hocemo da vratimo (2, 3)
	std::vector<int> out;
	out.push_back(rez.ff);
	out.push_back(rez.ss);
	return out;
}