Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

#author : dokueki

import sys


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def rev(x, limit):
    s = 0
    for i in range(len(x) - 1):
        if x[i] == 0:
            x[i] = limit
        s += abs(x[i] - x[i + 1])
        # print(s)
    return s


def main():
    k, n = map(int, sys.stdin.readline().split())
    a = list(map(int, sys.stdin.readline().split()))
    daf = a[-1] - a[0]
    minn = min(daf, int(10e10))
    for i in range(1, n):
        grab = [a[i]] + a[:i] + a[i + 1:]
        #print(grab)
        minn = min(rev(grab, k), minn)
    print(minn)


if __name__ == "__main__":
    #IOE()
    main()


### FIXED CODE ###

#author : dokueki

import sys


def IOE():
    sys.stdin = open("input.txt", "r")
    sys.stdout = open("output.txt", "w")


def main():
    k, n = map(int, sys.stdin.readline().split())
    a = list(map(int, sys.stdin.readline().split()))
    minn = min(a[n - 1] - a[0], int(10e10))
    for i in range(n):
        ans = (a[n - 1] - a[i]) + (k + a[i - 1] - a[n - 1])
        minn = min(ans, minn)
    print(minn)


if __name__ == "__main__":
    # IOE()
    main()
### BUGGY CODE ###

NK = input().split();
N,K = int(NK[0]),int(NK[1]);
min = N;

#桁が大きい時の処理
if(K == 1):
    while(int(len(str(N))) > 2):
        N = int(N - K * (N / 2));
# else:
#     while(int(len(str(N))) > 2):
#         N = int(N / K);
        
flag = True;
while flag == True:
    N = abs(N - K);
    if(N <= min):
        min = N;
    else:
        flag = False;
    
print(min);

### FIXED CODE ###

import math

NK = input().split();
N,K = int(NK[0]),int(NK[1]);
min = N;

#桁が大きい時の処理
if(K == 1):
    while(int(len(str(N))) > 2):
        N = int(N - K * (N / 2));
else:
    border = math.floor(N / K);
    N = N - K * border;
        
flag = True;
while flag == True:
    N = abs(N - K);
    if(N <= min):
        min = N;
    else:
        flag = False;
    
print(min);
### BUGGY CODE ###

import bisect

N, M = map(int, input().split())
A = list(map(int, input().split()))

counter = {}
values = []
for a in A:
    if a in counter:
        counter[a] += 1
    else:
        counter[a] = 1
keys = sorted(counter.keys())

for _ in range(M):
    B, C = map(int, input().split())
    b = 0
    new_keys = []
    for k in keys:
        c = counter[k]
        if b >= B or C <= k:
            break
        if b + c >= B:
            p = B - b
        else:
            p = c
        counter[k] -= p
        if C in counter:
            counter[C] += p
        else:
            counter[C] = p
        b += p
        new_keys.append(C)
    for nk in new_keys:
        pos = bisect.bisect_left(keys, nk)
        if len(keys) == pos:
            keys.append(nk)
        elif keys[pos] == nk:
            pass
        else:
            keys.insert(pos, nk)

total = 0
for k in keys:
    total += counter[k] * k
print(total)


### FIXED CODE ###

from collections import defaultdict

N, M = map(int, input().split())
# Aは昇順に並べる
A = list(sorted(map(int, input().split())))

# B,Cの対応辞書を作り、C順に並べる
counter = defaultdict(int)
for _ in range(M):
    B, C = map(int, input().split())
    counter[C] += B
c_list = list(sorted(counter.keys(), reverse=True))

# Cを大きい順に個数分並べたリストを作る
moves = []
n = 0
for c in c_list:
    b = counter[c]
    if n + b < N:
        p = b
    else:
        p = N - n
    for i in range(p):
        moves.append(c)
    if len(moves) >= N:
        break

# AとCを小さい順に比較
siz = min(N, len(moves))
for i in range(siz):
    # Aの小さいものを上書き
    A[i] = max(A[i], moves[i])

print(sum(A))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10 ** 5)

N, Z, W = map(int, input().split())
a = list(map(int, input().split()))
a.reverse()
a += [W] #最後にYさんの手札を加えておく
dp_T = [-1] * (N + 1) #Xの手番
dp_F = [-1] * (N + 1) #Yの手番

def calc(n, flag): #n: 残りの山の枚数　flag: ターンの管理
    #既に計算済みの場合その値を返す
    # if flag == True and dp_T[n] != -1:
    #     return dp_T[n]
    # if flag == False and dp_F[n] != -1:
    #     return dp_F[n]

    if n == 1: #山の残り枚数が1枚の時 -->引くしかなく得点が決定
        return abs(a[0] - a[n])
    if flag == True: #Xのターン-->大きくしたい
        tmp = 0
        for i in range(1, n + 1): #自分が引く枚数 = 1 ~ n枚
            if i == n: #残り枚数0で相手に渡す = 自分が最後の1枚を引く -->相手の手札が決まっているから得点が決定
                tmp = max(tmp, abs(a[0]-a[n]))
            else: #残り枚数が1枚以上ある時、手番は相手に変える
                tmp = max(tmp, calc(n-i, not flag))
        dp_T[n] = tmp
        return tmp
    if flag == False: #Yのターン-->小さくしたい
        tmp = 10 ** 10 #最大値
        for i in range(1, n + 1): #自分が引く枚数 = 1 ~ n枚
            if i == n - 1: #残り0枚で相手に渡す
                tmp = min(tmp, abs(a[0] - a[n]))
            else:
                tmp = min(tmp, calc(n-i, not flag))
        dp_F[n] = tmp
        return tmp

print (calc(N, True))


### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 5)

N, Z, W = map(int, input().split())
a = list(map(int, input().split()))
a.reverse()
a += [W] #最後にYさんの手札を加えておく
dp_T = [-1] * (N + 1) #Xの手番
dp_F = [-1] * (N + 1) #Yの手番

def calc(n, flag): #n: 残りの山の枚数　flag: ターンの管理
    #既に計算済みの場合その値を返す
    if flag == True and dp_T[n] != -1:
        return dp_T[n]
    if flag == False and dp_F[n] != -1:
        return dp_F[n]

    if n == 1: #山の残り枚数が1枚の時 -->引くしかなく得点が決定
        return abs(a[0] - a[n])
    if flag == True: #Xのターン-->大きくしたい
        tmp = 0
        for i in range(1, n + 1): #自分が引く枚数 = 1 ~ n枚
            if i == n: #残り枚数0で相手に渡す = 自分が最後の1枚を引く -->相手の手札が決まっているから得点が決定
                tmp = max(tmp, abs(a[0]-a[n]))
            else: #残り枚数が1枚以上ある時、手番は相手に変える
                tmp = max(tmp, calc(n-i, not flag))
        dp_T[n] = tmp
        return tmp
    if flag == False: #Yのターン-->小さくしたい
        tmp = 10 ** 10 #最大値
        for i in range(1, n + 1): #自分が引く枚数 = 1 ~ n枚
            if i == n - 1: #残り0枚で相手に渡す
                tmp = min(tmp, abs(a[0] - a[n]))
            else:
                tmp = min(tmp, calc(n-i, not flag))
        dp_F[n] = tmp
        return tmp

print (calc(N, True))
### BUGGY CODE ###

import collections

n,k=(int(x) for x in input().split())
a = [int(x) for x in input().split()]

for x in range(k):
    b = []
    for i,j in enumerate(a):
        b.extend(range(max(i-j,0),min(i+j+1,n)))
    c = collections.Counter(b)
    a = c.values()
    
print(" ".join([str(x) for x in a]))

### FIXED CODE ###

import math
import numpy as np

n,k=(int(x) for x in input().split())
a = [int(x) for x in input().split()]


for _ in range(k):
    new_a = np.zeros(n+1,dtype=int)
    np.add.at(new_a, np.maximum(0,np.arange(0, n)-a), 1)
    np.add.at(new_a, np.minimum(n,np.arange(0, n)+a+1), -1)
    a = new_a.cumsum()[:-1]
    if np.all(a==n):
        break

print(" ".join([str(x) for x in a]))
### BUGGY CODE ###

X,K,D = map(int,input().split())
List = []

if abs(X) > K*D:
    print(abs(X) - K*D)
elif K == 1:
    print(min(abs(X-D),abs(X+D)))
elif K == 2:
    print(min(abs(X+D+D),abs(X-D-D),abs(X)))
elif K == 3:
    print(min(abs(X+D+D+D),abs(X+D),abs(X-D),abs(X-D-D-D)))
else:
    for i in range(3):
        if abs(X - D) >= abs(X+D):
            List.append(abs(X+D))
            X = X+D
        else:
            List.append(abs(X-D))
            X = X-D

    for i in range(K-3):
        if List[i] == List[i+2]:
            if K%2 == i%2:
                print(List[i+1])
            else:
                print(List[i])
            break
        else:
            if abs(X - D) >= abs(X+D):
                List.append(abs(X+D))
                X = X+D
            else:
                List.append(abs(X-D))
                X = X-D

    if len(List) == K:
        print(List[K-1])
    else:
        pass

### FIXED CODE ###

X,K,D = map(int,input().split())

t = abs(X) // D
s = abs(X) % D

if abs(X) > K*D:
    print(abs(X) - K*D)
elif t%2 == K%2:
    print(s)
else:
    print(D-s)
### BUGGY CODE ###

a, b, m = map(int, input().split())
ref = list(map(int, input().split()))
micro = list(map(int, input().split()))
tk = [list(map(int, input().split())) for i in range(m)]

amount = set()
for i in range(a):
    for j in range(b):
        amount.add(ref[i] + micro[j])


for i in tk:
    amount.add(ref[i[0]-1] + micro[i[1]-1] - i[2])

print(min(amount))

### FIXED CODE ###

a, b, m = map(int, input().split())
ref = list(map(int, input().split()))
micro = list(map(int, input().split()))
tk = [list(map(int, input().split())) for i in range(m)]

mini = min(ref) + min(micro)

amount = 10 ** 6
for i in tk:
    if ref[i[0]-1] + micro[i[1]-1] - i[2] < amount:
        amount = ref[i[0]-1] + micro[i[1]-1] - i[2]

print(min(mini, amount))
### BUGGY CODE ###

N, M = map(int, input().split())

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

uf = UnionFind(N)

for _ in range(M):
    A, B = map(int, input().split())
    A = A - 1
    B = B - 1
    uf.union(A, B)
    uf.find(A)

ans = 0
for k, v in uf.all_group_members().items():
    ans = max(ans, len(v))
print(ans)

### FIXED CODE ###

class UnionFind:
    def __init__(self, N):
        self.parent = list(range(N))
        self.rank = [0] * N
        self.size = [1] * N

    def find(self, x):
        r = x
        while self.parent[r] != r:
            r = self.parent[r]
        while self.parent[x] != r:
            x, self.parent[x] = self.parent[x], r
        return r

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
            self.size[y] += self.size[x]
        else:
            self.parent[y] = x
            self.size[x] += self.size[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        return self.size[self.find(x)]

N, M = map(int, input().split())
uf = UnionFind(N)
for _ in range(M):
    A, B = map(int, input().split())
    uf.unite(A-1, B-1)

ans = max(uf.get_size(i) for i in range(N))
print(ans)
### BUGGY CODE ###

n = int(input())
nums = list(map(int, input().split()))
sum = 1
flag = 1
upper = pow(10, 18)
for num in nums:
  sum = sum * num
  if sum > upper:
    flag = 2
  if num == 0:
    sum = 0
    flag = 0
if flag == 0:
  print(0)
elif flag == 1:
  print(sum)
elif flag == 2:
  print(-1)

### FIXED CODE ###

n = int(input())
nums = list(map(int, input().split()))
sum = 1
flag = 1
upper = pow(10, 18)
for num in nums:
  if num == 0:
    sum = 0
    flag = 1
    break
  if flag == 1:
    sum = sum * num
    if sum > upper:
      flag = 2

if flag == 1:
  print(sum)
elif flag == 2:
  print(-1)
### BUGGY CODE ###

import sys
import math
k = int(sys.stdin.readline())
a = 1
t = 0
while a <= k:
  b = 1
  while b <= k:
    c = 1
    while c <= k:
      t += math.gcd(math.gcd(a,b), c)
      c += 1
    b += 1
  a += 1
print(t)


### FIXED CODE ###

import sys
import math
k = int(sys.stdin.readline())
a = 1
t = 0
while a <= k:
  b = 1
  while b <= k:
    c = 1
    while c <= k:
      t += math.gcd(math.gcd(a,b), c)
      c += 1
    b += 1
  a += 1
print(t)
### BUGGY CODE ###

def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result
    
    
N = int(input())
A = list(map(int, input().strip().split()))
from collections import Counter
C=Counter(A)
K=set(A)

culc = {}
# print(K)
# print(C)

for n in K:
    tmp = 0
    # print("n=",n)
    for i in C:
        if C[i] != 1:
            if i == n and C[i]-1 == 1:
                continue
            elif i == n :
                tmp += cmb(C[i]-1,2)
            else:
                tmp += cmb(C[i],2)
        # print("C=",i,"count",C[i],tmp)
    # print("K=",n,"の時、",tmp)
    culc[n]=tmp

# print(culc)

for i in range(N):
    print(culc[A[i]])
    

### FIXED CODE ###

N = int(input())
A = list(map(int, input().strip().split()))
from collections import Counter
C=Counter(A)
K=set(A)
ans = 0
for i in K:
    ans += C[i]*(C[i]-1)//2

for i in A:
    print(ans-(C[i]-1)) # nシグマk = n(n-1)/2 ......
### BUGGY CODE ###

#/usr/bin/env python
from functools import *
from math import *

n = int(input())
a = list(map(int, input().split()))

gcds = []
for i in range(n):
    l = a[:i] 
    r = a[i+1:]
    if len(l) == 0:
        gl = 0 
    else:
        gl = reduce(gcd, l)

    if len(r) == 0:
        gr = 0 
    else:
        gr = reduce(gcd, r)

    g = gcd(gl, gr) 
    gcds.append(g)

print(max(gcds))


### FIXED CODE ###

#/usr/bin/env python
from functools import *
from math import *

n = int(input())
a = list(map(int, input().split()))

gcds = []
lgcd = []
rgcd = [0 for _ in range(n)]
l = r = 0 
for i in range(n):
    if i == 0:
        l = 0 
    else:
        l = gcd(l, a[i-1])
    lgcd.append(l)

for i in range(n):
    if i == 0:
        r = 0 
        rgcd[n-1] = r 
    else:
        r = gcd(r, a[n-i])
        rgcd[n-i-1] = r 

for i in range(n):
    g = gcd(lgcd[i], rgcd[i])
    gcds.append(g)

print(max(gcds))
### BUGGY CODE ###

N = int(input())
H = [int(j) for j in input().split()]
maximum = 0
i = 0
for i in range(N-1):
    cnt = 0

    while H[i] >= H[i+1] and i < N-1:

        cnt+=1
        i += 1
        if maximum < cnt:
            maximum = cnt
            
        if i == N-1:
            break
        
print(maximum)

### FIXED CODE ###

N = int(input())
H = [int(j) for j in input().split()]
maximum = 0
cnt = 0
for i in range(0,N-1):
    
    if H[i] >=H[i+1]:
        cnt+=1
        if maximum < cnt:
            maximum = cnt
    else:
        cnt=0
        
print(maximum)
### BUGGY CODE ###

n = int(input())
count = 0

while(n>0):
    count += n%10
    n = n//10

if(count%9 == 0):
    print("Yes")
else:
    print("No")


### FIXED CODE ###

n = int(input())

if(n%9 == 0):
    print("Yes")
else:
    print("No")
### BUGGY CODE ###

n = int(input())

v = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = 0

for i in range(1 << n):
	tmp = 0
	for j in range(n):
		if(i >> j & 1):
			tmp += v[j] - c[j]
	ans = max(ans, tmp)

print(ans)

### FIXED CODE ###

n = int(input())

v = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = 0

for i in range(n):
	if v[i] - c[i] > 0:
		ans += v[i] - c[i]

print(ans)
### BUGGY CODE ###

import math
n,m = map(int,input().split())
ans = 1
for i in range(2,m//2+1):
    if m % i == 0:
        if m // i >= n:
            ans = i
if m % m == 0:
    if m // m >= n:
        ans = m
print(ans)

### FIXED CODE ###

import math
n,m = map(int,input().split())

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

divisor_ls = make_divisors(m)

ans = 1
for divisor in divisor_ls:
    if m // divisor < n:
        break
    else:
        ans = divisor

print(ans)
### BUGGY CODE ###

N, M = map(int, input().split())

G = {i:[] for i in range(1, N + 1)}

for i in range(M):
    a, b = map(int, input().split())
    G[a].append(b)
    G[b].append(a)

res = False

for obj in G[1]:
    if obj in G[N]:
        res = True
        break


if res:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")

### FIXED CODE ###

N, M = map(int, input().split())

G = {i: [] for i in range(1, N + 1)}

for i in range(M):
    a, b = map(int, input().split())
    if a == 1 or a == N or b == 1 or b == N:
        G[a].append(b)
        G[b].append(a)

res = False

for i in range(2, N):
    if 1 in G[i] and N in G[i]:
        res = True
        break


if res:
    print("POSSIBLE")
else:
    print("IMPOSSIBLE")
### BUGGY CODE ###

#bin()やformat(数字 ,"b")を使うとstr型になる
#bin()とformat(数字 ,"#b")は同意

N = int(input())
A = list(map(int, input().split()))

for i in range(N): A[i] = format(A[i], "060b") #ここで全て60ビットの二進数に変換

mod = 10**9+7

sum = 0
for i in range(1,61):
    count0 = 0
    count1 = 0
    for j in range(N):
        if A[j][-i] == "0": #str型であることに注意！
            count0 += 1
        else:
            count1 += 1 
    sum += (count0 * count1) << (i-1) #左シフトだが，2**(i-1)でもOK
print(sum%mod)


### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
mod = 10**9+7
xorsum = 0
for i in range(60):
  c1 = sum([1 for a in A if (a >> i) & 1]) #
  xorsum += (c1*(N-c1)*(1<<i))
  xorsum = xorsum%mod
print(xorsum%mod)
### BUGGY CODE ###

k, q = map(int, input().split())
d = list(map(int, input().split()))
nxm = [list(map(int, input().split())) for i in range(q)]
for n, x, m in nxm:
    prev = x
    ans = 0
    cur = 0
    for i in range(k):
        prev += (d[i] % m) * ((n + k - i  - 2) // k)
        if d[i] % m == 0:
            cur += ((n + k - i  - 2) // k)
    print(n - prev // m + x // m - cur - 1)

### FIXED CODE ###

k, q = map(int, input().split())
d = list(map(int, input().split()))
nxm = [list(map(int, input().split())) for i in range(q)]
for n, x, m in nxm:
    prev = x
    ans = 0
    cur = 0
    for i in range(k):
        prev += (d[i] % m) * ((n + k - i  - 2) // k)
        if d[i] % m == 0:
            cur += ((n + k - i  - 2) // k)
    print(n - prev // m + x // m - cur - 1)
### BUGGY CODE ###

import math

K=int(input())

ans = 0
for i in range(K):
    for j in range(K):
        for k in range(K):
            ans+=math.gcd(i+1,math.gcd(j+1,k+1))


print(ans)

### FIXED CODE ###

import math

K=int(input())

ans = 0
for i in range(K):
    for j in range(K):
        for k in range(K):
            ans+=math.gcd(i+1,math.gcd(j+1,k+1))


print(ans)
### BUGGY CODE ###

N = int(input())
S = []
dic = {}
for i in range(N):
    a = str(input())
    if a not in S:
        S.append(a)
        dic[a] = 1
    if a in S:
        dic[a] += 1
max_list = [kv[0] for kv in dic.items() if kv[1] == max(dic.values())]
max_list.sort()
for a in max_list:
    print(a)

### FIXED CODE ###

N = int(input())
S = [str(input()) for i in range(N)]
dic = {}
for a in S:
    if a not in dic:
        dic[a] = 1
    else:
        dic[a] += 1
m = max(dic.values())
max_list = [kv[0] for kv in dic.items() if kv[1] == m]
max_list.sort()
for a in max_list:
    print(a)
### BUGGY CODE ###

N,M = map(int,input().split())
S = [list(map(int,input().split())) for m in range(M)]
T = [s for s in S if 1 in s or N in s]
ans = "IMPOSSIBLE"

for n in range(1+N):
  if [1,n] in T and [n,N] in T:
    ans = "POSSIBLE"

print(ans)

### FIXED CODE ###

N,M = list(map(int,input().split()))
x = 1
y = 1

for m in range(M):
    a,b = list(map(int,input().split()))
    if a==1:
      x|=1<<b
    if b==N:
      y|=1<<a

print("IM"*(x&y==1)+"POSSIBLE")
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
n = len(A)
ans = 0

for i in range(n):
    for j in range(n):
        if i < j:
            ans += A[i] * A[j]
            ans = ans % (10 ** 9 + 7)


print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
n = len(A)
z = 10 ** 9 + 7
ans = 0

x = sum(A)
x = x ** 2
for i in A:
    y = i * i
    x -= y

x = x // 2
x = x % z
print(x)
### BUGGY CODE ###

import queue


def up(y, x, d):
    return y-1, x, d


def down(y, x, d):
    return y+1, x, d


def left(y, x, d):
    return y, x-1, d


def right(y, x, d):
    return y, x+1, d


def put_all(q, y, x, d):
    q.put((up(y, x, d)))
    q.put((down(y, x, d)))
    q.put((left(y, x, d)))
    q.put((right(y, x, d)))
    return


def put_not_all(q, y, x, d, distances):
    if distances[y-1][x] != -1:
        q.put((y-1, x, d))
    if distances[y+1][x] != -1:
        q.put((y+1, x, d))
    if distances[y][x-1] != -1:
        q.put((y, x-1, d))
    if distances[y][x+1] != -1:
        q.put((y, x+1, d))


def solve(maze, init_y, init_x, distances, H, W):
    if maze[init_y][init_x] == "#":
        return 0

    distances[init_y][init_x] = 0

    q = queue.Queue()
    put_all(q, init_y, init_x, 1)

    farthest = 0
    while not q.empty():
        y, x, d = q.get()
        # print(x, y)
        if x < 0 or x >= W or y < 0 or y >= H:
            # print("continue")
            continue
        if maze[y][x] == "#":
            distances[y][x] = 0
            continue
        distance = distances[y][x]
        if distance == -1:
            distances[y][x] = d
            put_all(q, y, x, d+1)
            farthest = max(farthest, d)

    return farthest


def main():
    H, W = map(int, input().split())
    maze = [input() for _ in range(H)]

    max_distance = 0
    for i in range(H):
        for j in range(W):
            initial_map = [[-1 for _ in range(W)] for _ in range(H)]
            distance = solve(maze, i, j, initial_map, H, W)
            max_distance = max(distance, max_distance)
    print(max_distance)


if __name__ == '__main__':
    main()


### FIXED CODE ###

def d_maze_master(INF=float('inf')):
    from collections import deque
    H, W = [int(i) for i in input().split()]
    Maze = [list(input()) for _ in range(H)]

    def bfs(start_row, start_col):
        diff = ((-1, 0), (0, 1), (1, 0), (0, -1))

        def is_inside(row, col):
            return 0 <= row < H and 0 <= col < W

        queue = deque([(start_row, start_col)])
        dist = [[INF for _ in range(W)] for _ in range(H)]
        dist[start_row][start_col] = 0
        while queue:
            row, col = queue.pop()
            for dr, dc in diff:
                nr, nc = row + dr, col + dc
                if is_inside(nr, nc) and Maze[nr][nc] != '#' and dist[nr][nc] == INF:
                    queue.appendleft((nr, nc))
                    dist[nr][nc] = dist[row][col] + 1
        return dist

    ans = 0
    for start_row in range(H):
        for start_col in range(W):
            if Maze[start_row][start_col] == '#':
                continue
            dist = bfs(start_row, start_col)
            for row in range(H):
                for col in range(W):
                    if Maze[row][col] != '#':
                        ans = max(ans, dist[row][col])
    return ans

print(d_maze_master())