using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.Linq; using System.Text; using System.IO; using System.Reflection; using static System.Math; using System.Numerics; using System.Threading; using System.Runtime.CompilerServices; static class Program{ const int mod=(int)1e9+7; static List[] li; static bool[] b; static int[] h; static void Main(){ Sc sc=new Sc(); var s=sc.Ia; int n=s[0]*s[0],m=s[1]; li=new List[n]; b=new bool[n]; h=new int[n]; var c1=new long[n]; var c2=new long[n]; for(int i=0;i(); if(i>=s[0]){ li[i].Add(i-s[0]); li[i-s[0]].Add(i); } if(i%s[0]!=0){ li[i].Add(i-1); li[i-1].Add(i); } } for(int i=0;i(32); pq.Push(new Rheap.Dt(0,0)); while(pq.cnt>0){ var e=pq.Dequeue; if(b[e.d]){continue;} b[e.d]=true; c1[e.d]=e.n; for(int i=0;i.Dt(e.n+h[li[e.d][i]]+1,li[e.d][i])); } } } b=new bool[n]; pq=new Rheap(32); pq.Push(new Rheap.Dt(0,n-1)); while(pq.cnt>0){ var e=pq.Dequeue; if(b[e.d]){continue;} b[e.d]=true; c2[e.d]=e.n; for(int i=0;i.Dt(e.n+h[li[e.d][i]]+1,li[e.d][i])); } } } long ans=long.MaxValue; for(int i=0;i{ public class Dt{ public long n; public T d; public Dt(long n,T d){this.n=n;this.d=d;} public override string ToString()=>"d:"+d.ToString()+" n:"+n.ToString(); } private const int m=65; private long la=0; private Dt[][] bf; private int[] h; public int cnt=0; public Rheap(int max){ bf=new Dt[m][]; h=new int[m]; for(int i = 0;i0){ int p=0; for(int j = 1;jbf[i][j].n){p=j;} } la=bf[i][p].n; for(int j = 0;j(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i