#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;

ll ask(ll xx,ll yy){
	return 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;
}

std::pair <int, int> find_locmin_point(int _n, int _m) {
	n=_n;
	m=_m;
	for(int i=0;i<n+5;++i){
		for(int j=0;j<m+5;++j)
			a[i][j]=0;
	}

	ll xx=rng();
	xx=max(xx,-xx);
	xx%=n;++xx;
	ll yy=rng();
	yy=max(yy,-yy);
	yy%=m;++yy;

	a[xx][yy]=ask(xx,yy);

	while(1){
		vector<array<ll,2>>lk=okolo(xx,yy);
		bool ok=0;
		while(lk.size()){
			ll id=rng();id=max(id,-id);id%=(ll)lk.size();
			ll nx=lk[id][0],ny=lk[id][1];
			a[nx][ny]=ask(nx,ny);
			if(a[nx][ny]<a[xx][yy]){
				xx=nx;
				yy=ny;
				ok=1;
				break;
			}
			lk.erase(lk.begin()+id);
		}
		if(!ok){
			return{(int)xx,(int)yy};
		}
	}
}