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

//long long value(int r, int c)

mt19937_64 rng(time(0));
const ll N=555;
ll a[N][N],n,m;

void ask(ll xx,ll yy){
	if(a[xx][yy])return;
	a[xx][yy]=value((int)xx,(int)yy);
}

vector<array<ll,2>>okolo(ll xx,ll yy){
	vector<array<ll,2>>ret;
	if(xx-1>=1&&!a[xx-1][yy])ret.push_back({xx-1,yy});
	if(xx+1<=n&&!a[xx+1][yy])ret.push_back({xx+1,yy});
	if(yy-1>=1&&!a[xx][yy-1])ret.push_back({xx,yy-1});
	if(yy+1<=m&&!a[xx][yy+1])ret.push_back({xx,yy+1});
	return ret;
}

bool chk(ll i,ll j){
	return(a[i][j]<min({a[i-1][j],a[i+1][j],a[i][j-1],a[i][j+1]}))
}

std::pair <int, int> find_locmin_point(int _n, int _m){
	n=_n;
	m=_m;
	ll l=1,r=m,u=1,d=n;
	for(int i=0;i<n+4;++i){
		for(int j=0;j<m+4;++j)
			a[i][j]=0;
	}
	for(int i=0;i<n+4;++i)
		a[i][0]=a[i][m+1]=2e18;
	for(int i=0;i<m+4;++i)
		a[0][i]=a[n+1][i]=2e18;
	while(l<=r&&u<=d){
		ll md1=(l+r)/2;
		ll md2=(u+d)/2;
		vector<array<ll,2>>lk;
		for(ll i=u;i<=d;++i){
			lk.push_back({i,md1});
		}
		for(ll i=l;i<=r;++i){
			lk.push_back({md2,i});
		}
		for(auto[u,v]:lk)
			ask(u,v);
		array<ll,2>koi=lk[0];
		for(auto[u,v]:lk){
			if(a[u][v]<a[koi[0]][koi[1]])
				koi={u,v};
		}
		vector<array<ll,2>>okl=okolo(koi[0],koi[1]);
		for(auto[u,v]:okl)
			ask(u,v);
		bool ok=1;
		for(auto[u,v]:okl)
			ok&=(a[koi[0]][koi[1]]<a[u][v]);
		if(ok&&chk(koi[0],koi[1])){
			return{(int)koi[0],(int)koi[1]};
		}
		array<ll,2>nv=okl[0];
		for(auto[u,v]:okl){
			if(a[u][v]<a[nv[0]][nv[1]])
				nv={u,v};
		}
		if(koi[1]==md1){
			if(koi[0]>md2){
				u=md2+1;
			}else{
				d=md2-1;
			}
			if(nv[1]>md1){
				l=md1+1;
			}else
				r=md1-1;
		}else{
			if(koi[1]>md1){
				l=md1+1;
			}else
				r=md1-1;
			if(nv[0]<md2){
				d=md2-1;
			}else
				u=md2+1;
		}
	}
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j){
			if(chk(i,j))
				return{i,j};
		}
	}
	exit(-1);
}