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

#define MAXN 501

long long matrix[MAXN][MAXN];

std::pair <int, int> find_locmin_point(int N, int M)
{
    int currx=1,curry=1;long long curr=value(1,1);matrix[1][1]=curr;
    while (true)
    {
        vector<pair<long long,pair<int,int>>> values;
        if (currx!=1)
        {
            if (matrix[currx-1][curry]!=0) values.push_back({matrix[currx-1][curry],{currx-1,curry}});
            else {int v=value(currx-1,curry);values.push_back({v,{currx-1,curry}});matrix[currx-1][curr]=v;}
        }
        if (currx!=N)
        {
            if (matrix[currx+1][curry]!=0) values.push_back({matrix[currx+1][curry],{currx+1,curry}});
            else {int v=value(currx+1,curry);values.push_back({v,{currx+1,curry}});matrix[currx+1][curry]=v;}
        }
        if (curry!=1)
        {
            if (matrix[currx][curry-1]!=0) values.push_back({matrix[currx][curry-1],{currx,curry-1}});
            else {int v=value(currx,curry-1);values.push_back({v,{currx,curry-1}});matrix[currx][curry-1]=v;}
        }
        if (curry!=M)
        {
            if (matrix[currx][curry+1]!=0) values.push_back({matrix[currx][curry+1],{currx,curry+1}});
            else {int v=value(currx,curry+1);values.push_back({v,{currx,curry+1}});matrix[currx][curry+1]=v;}
        }
        sort(values.begin(),values.end());
        if (values[0].first>curr) return {currx,curry};
        currx=values[0].second.first,curry=values[0].second.second;curr=values[0].first;
    }
}