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

#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)

def find(LI,i,j,p):
    data=[(0,1),(0,-1),(1,0),(-1,0)]
    if not (0<=i<h and 0<=j<w):
        return -1

    if s[i][j]=="#":
        LI[i][j] = -1
        return LI[i][j]

    if LI[i][j]!=-2:
        return LI[i][j]
    else:
        LI[i][j]= find(LI, i+data[p][0], j+data[p][1], p)+1
        return LI[i][j]
    

h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
R=[[-2]*w for i in range(h)]
L=[[-2]*w for i in range(h)]
F=[[-2]*w for i in range(h)]
B=[[-2]*w for i in range(h)]
ans=0
for i in range(h):
    for j in range(w):
        find(R,i,j,0)
        find(L,i,j,1)
        find(F,i,j,2)
        find(B,i,j,3)
        if s[i][j]!="#":
            ans = max(ans, sum((R[i][j], L[i][j], F[i][j], B[i][j]))+1)
print(ans)



### FIXED CODE ###

h,w=map(int,input().split())
data=[list(input()) for i in range(h)]
yoko=[[0]*w for i in range(h)]
for i in range(h):
	j=0
	while j <w:
		if data[i][j]=='#':
			j+=1
		else:
			start=j
			while j<w-1:
				j+=1
				if data[i][j]=='#':
					for k in range(start,j):
						yoko[i][k]=j-start-1
					j+=1
					break
			else:
				j+=1
				for k in range(start,j):
					yoko[i][k]=j-start-1
				j+=1
# for i in range(h):
# 	print(yoko[i])

for j in range(w):
	i=0
	while i <h:
		if data[i][j]=='#':
			i+=1
		else:
			start=i
			while i<h-1:
				i+=1
				if data[i][j]=='#':
					for k in range(start,i):
						yoko[k][j]+=i-start-1
					i+=1
					break
			else:
				i+=1
				for k in range(start,i):
					yoko[k][j]+=i-start-1
				i+=1

# print(yoko)
for i in range(h):
	# print(yoko[i])
	yoko[i]=max(yoko[i])
print(max(yoko)+1)
### BUGGY CODE ###

n,m=map(int,input().split())
a = sorted([int(i) for i in input().split()])[::-1]
ans = 0

while m > 0:
    a[0] = a[0]//2
    m-=1
    for i in range(n-1):
        if a[i] < a[i+1]: a[i],a[i+1] = a[i+1],a[i]
        else: break
print(sum(a))

### FIXED CODE ###

import heapq

n,m=map(int,input().split())
a = sorted([int(i) for i in input().split()])
a = list(map(lambda x: x*(-1), a))
heapq.heapify(a)

while m > 0:
    t = heapq.heappop(a)*(-1)//2
    heapq.heappush(a,-t)
    m-=1
print(-sum(a))
### BUGGY CODE ###

n = int(input())

p = list(map(int,input().split()))

for x in range(1,n+1):
  print(p.count(x))
  


### FIXED CODE ###

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

p2 = [0]*n 

for x in p:
    p2[x-1] = p2[x-1] + 1

for x in p2:
    print(x)
### BUGGY CODE ###

S = input()[::-1]
dp = [0]*2019
dp[0] = 1
for i in range(1,len(S)+1):
    dp[int(S[:i][::-1])%2019] += 1
print(sum([i*(i-1)//2 for i in dp]))

### FIXED CODE ###

P = 2019
S = input()
N = len(S)
res = 0
 
if P in [2,5]:
    for i in range(N):
        if int(S[i])% P == 0:
            res += i+1
else:
    dic = {0:1}
    t1 = 0
    t2 = 1
    for i in range(N):
        t2 = (10*t2) % P
        t1 = (int(S[N-1-i])*t2 + t1) % P
        if t1 in dic:
            res += dic[t1]
            dic[t1] += 1
        else:
            dic[t1] = 1
 
print(res)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
S = input()
judge = [0] * 1
tmp = ''
ans = 0
m = ''

for n in range(0, len(S)):
	m += S[n]
	judge[0] = m

	if ans > len(S) - len(m) + 1:
		print(ans)
		exit()
	#print(judge)

	for i in range(len(m), len(S)):
		tmp += S[i]
		
		for j in range(len(judge) - 1, len(judge)):
			if judge[j] == tmp:
				break

		else:
			judge.append(tmp)
			tmp = ''
	
	else:
		ans = max(ans, len(judge))
		judge = [0] * 1

print(ans)

### FIXED CODE ###

S = input()
dp = [0] * (len(S) + 1)
dp[1] = 1

if S[0] == S[1]:
	dp[2] = 1
else:
	dp[2] = 2

for i in range(3, len(S) + 1):
	if S[i - 1] != S[i - 2]:
		dp[i] = dp[i - 1] + 1
	else:
		dp[i] = dp[i - 3] + 2

print(dp[-1])
### BUGGY CODE ###

def main():

    N, K = map(int, input().split())
    H = list(map(int, input().split()))
    dp = [float('inf') for _ in range(N)]
    dp[0] = 0
    for i in range(1, N):
        for j in range(1, K+1):
            if i - j >=0:
                dp[i] = min(dp[i], dp[i-j] + abs(H[i] - H[i-j]))

    return dp[N-1]

if __name__ == '__main__':
    print(main())

### FIXED CODE ###

def main():

    N, K = map(int, input().split())
    H = list(map(int, input().split()))
    dp = [float('inf') for _ in range(N)]
    dp[0] = 0
    for i in range(1, N):
        for j in range(1, K+1):
            if i - j >=0:
                dp[i] = min(dp[i], dp[i-j] + abs(H[i] - H[i-j]))

    return dp[N-1]

if __name__ == '__main__':
    print(main())
### BUGGY CODE ###

def factorization(n):
    if n == 1:
        return 1
    arr = 1
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr *= (cnt + 1)

    if temp!=1:
        arr *= 2

    if arr==[]:
        arr *= 2

    return n * arr
n = int(input())
res = 0
for i in range(1, n + 1):
  res += factorization(i)
print(res)

### FIXED CODE ###

n = int(input())
res = 0
for i in range(1, n + 1):
  y = n // i
  res += ((y * (y + 1) * i) // 2)
print(res)
### BUGGY CODE ###

import bisect
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
l=0
r=n
while l!=r:
    i=(l+r)//2
    ta = a[i]
    if ta>=k:flag=True
    else:
        na = a[:i]+a[i+1:]
        dp = [False for _ in range(k+1)]
        dp[0]=True
        for j in range(n-1):
            for m in range(k,0,-1):
                if m-na[j]>=0:
                    dp[m]=dp[m] or dp[m-na[j]]
        flag = False
        for j in range(k-ta,k):
            if dp[j]:
                flag = True
                break
    if flag:
        r=i
    else:
        l=i+1
print(l)



### FIXED CODE ###

import bisect
def main():
    n,k=map(int,input().split())
    a=list(map(int,input().split()))
    a.sort()
    l=0
    r=n
    d = {}
    if sum(a)<k:
        print(n)
    else:
        while l!=r:
            i=(l+r)//2
            ta = a[i]
            if ta in d.keys():
                flag = d[ta]
            elif ta>=k:flag=True
            else:
                flag = False
                na = a[:i]+a[i+1:]
                dp = [False for _ in range(k+1)]
                dp[0]=True
                for j in range(n-1):
                    for m in range(k,0,-1):
                        if m-na[j]>=0:
                            dp[m]=dp[m] or dp[m-na[j]]
                        if m != k and m>=k-ta and dp[m]:
                            flag = True
                            break
                    if flag:break
                d[ta] = flag
            
            if flag:
                r=i
            else:
                l=i+1
        print(l)

main()
### BUGGY CODE ###

import statistics
N=int(input())
X=list(map(int, input().split()))
for i in range(0,N):
    print(statistics.median(X[:i]+X[i+1:]))

### FIXED CODE ###

N=int(input())
X=list(map(int, input().split()))
sortX=sorted(X)
small=sortX[int(N/2)-1]
big=sortX[int(N/2)]
if small==big:
    for i in range(0,N):
        print(small)
    exit()

for i in range(0,N):
    if X[i]>small:
        print(small)
    else:
        print(big)
### BUGGY CODE ###

from collections import Counter

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

A_sort = sorted(A)
A_counter = Counter(A)
# print(A_sort)

res = 0
# res_list = []
for i in range(len(A_sort)):
    A_sort_tmp = A_sort[:i]

    if A_counter[A_sort[i]] >= 2:
        continue

    for j in range(len(A_sort_tmp)):
        if A_sort[i] % A_sort_tmp[j] == 0:
            break
    else:
        res += 1
        # res_list.append(i + 1)

print(res)
# print(res_list)


### FIXED CODE ###

from collections import defaultdict

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

bool_list = [True] * (10 ** 6 + 1)

duplicate_dic = defaultdict(int)
for a in A:
    duplicate_dic[a] += 1
    if duplicate_dic[a] > 1:
        bool_list[a] = False

for a in A_sort:
    for i in range(2*a, 10 ** 6 + 1, a):
        bool_list[i] = False

print(sum([1 for a in A_sort if bool_list[a]]))
### BUGGY CODE ###

n = int(input())
takahashi_u = []
aoki_u = []
sum_u = []
takahashi_eat = []
aoki_eat = []
for i in range(n):
  a, b = map(int, input().split())
  takahashi_u.append(a)
  aoki_u.append(b)
  sum_u.append(a+b)
for i in range(n):
  turn = i % 2
  # takahashi
  m_index = sum_u.index(max(sum_u))
  sum_u.pop(m_index)
  if turn == 0:
    takahashi_eat.append(takahashi_u.pop(m_index))
    aoki_u.pop(m_index)
  # aoki
  else:
    aoki_eat.append(aoki_u.pop(m_index))
    takahashi_u.pop(m_index)
print(sum(takahashi_eat) - sum(aoki_eat))

### FIXED CODE ###

import numpy as np
n = int(input())
takahashi_u = []
aoki_u = []
sum_u = []
takahashi_eat = []
aoki_eat = []
for i in range(n):
  a, b = map(int, input().split())
  takahashi_u.append(a)
  aoki_u.append(b)
  sum_u.append(a+b)
arr = np.array([sum_u, takahashi_u, aoki_u])
sort_index = np.argsort(arr[0])[::-1]
sort_arr = arr[:, sort_index]
takahashi_arr = sort_arr[1, 0::2]
aoki_arr = sort_arr[2, 1::2]
print(sum(takahashi_arr) - sum(aoki_arr))
### BUGGY CODE ###

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

res = 0; i = 0
while True:
    if max(H) == 0:
        break

    while i < N:
        if H[i] == 0:
            i += 1
        else:
            res += 1
            while i < N and H[i] > 0:
                H[i] -= 1
                i += 1
print(res)


### FIXED CODE ###

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

def flower(lst=H):
    if not lst:
        return 0
    ans = min(lst)
    lst = list(map(lambda x:x-ans, lst))
    indexes = [-1]
    indexes.extend([i for i, j in enumerate(lst) if j == 0])
    m = len(indexes)
    indexes.append(len(lst))
    for i in range(m):
        ans += flower(lst[indexes[i]+1:indexes[i+1]])
    return ans

print(flower())
### BUGGY CODE ###

import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


# Nの素因数分解を辞書で返す
def prime_fact(n):
    root = int(math.sqrt(n))
    prime_dict = {}
    for i in range(2, root+1):
        cnt = 0
        for j in range(root+1):
            if n % i == 0 and n != 0:
                cnt += 1
                n = n // i
            else:
                break
        if cnt:
            prime_dict[i] = cnt
    if n != 1:
        prime_dict[n] = 1
    return prime_dict


def main():
    N = NI()
    A = NLI()
    L = {}
    B = [0] * N
    primes_a = {}
    for a in A:
        prime_a = prime_fact(a)
        primes_a[a] = prime_a
        for p, n in prime_a.items():
            L.setdefault(p, 0)
            L[p] = max(L[p], n)
    for i, a in enumerate(A):
        b = 1
        for p, n in L.items():
            primes_a[a].setdefault(p, 0)
            b = b * pow(p, (n - primes_a[a][p]), MOD) % MOD
        B[i] = b
    print(sum(B) % MOD)



if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


# Nの素因数分解を辞書で返す
def prime_fact(n):
    root = int(math.sqrt(n))
    prime_dict = {}
    for i in range(2, root+1):
        cnt = 0
        for j in range(root+1):
            if n % i == 0 and n != 0:
                cnt += 1
                n = n // i
            else:
                break
        if cnt:
            prime_dict[i] = cnt
    if n != 1:
        prime_dict[n] = 1
    return prime_dict


def main():
    N = NI()
    A = NLI()
    L = {}
    primes_a = {}
    for a in A:
        prime_a = prime_fact(a)
        primes_a[a] = prime_a
        for p, n in prime_a.items():
            L.setdefault(p, 0)
            L[p] = max(L[p], n)
    ans = 0
    l = 1
    for p, n in L.items():
        l = l * pow(p, n, MOD) % MOD

    for i, a in enumerate(A):
        ans += l * pow(a, MOD-2, MOD) % MOD
    print(ans % MOD)



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

n = int(input())
L = list(map(int, input().split()))
L.sort()
count = 0
for i, l1 in enumerate(L):
    a = l1
    for k, l2 in enumerate(L[i+1:]):
        b = l2
        num = i + k + 2
        if num >= n:
            continue
        while L[num] < a + b:
            c = L[num]
            if a < b + c and b < a + c and c < b + a:
                count += 1
            num += 1
            if num == n:
                break

print(count)


### FIXED CODE ###

import bisect

n = int(input())
L = list(map(int, input().split()))
L.sort()
count = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        k = bisect.bisect_left(L[j+1:], L[i]+L[j])
        count += k

print(count)
### BUGGY CODE ###

def mincos(n,ab):
    aball=[]
    aball+=[(ab[i][0],i+1,'a') for i in range(n)]
    aball+=[(ab[i][1],i+1,'b') for i in range(n)]
    aball.sort()
    
    ans1=sum([i[0] for i in aball[:n]])
    
    hen=aball[n][0]
    hen2=aball[n+1][0]
    d=[]
    t=None
    hantei=True
    for i,c,mab in aball[:n]:
        if t!=None and t!=mab:
            hantei=False
        if  c in d:
            print(ans1)
            exit()
        d+=[c]
        t=mab

    if hantei:
        print(ans1)
        exit()

    ans2=[]
    for i,c,aorb in aball[:n]:
        if aball[n][1]!=c:
            ans2+=[ans1-i+hen]
        else:
            ans2+=[ans1-i+hen2]
    print(min(ans2))
    exit()

n=int(input())
print(mincos(n,[list(map(int,input().split())) for i in range(n)]))

### FIXED CODE ###

def mincos(n,ab):
    aball=[]
    aball+=[(ab[i][0],i+1,'a') for i in range(n)]
    aball+=[(ab[i][1],i+1,'b') for i in range(n)]
    aball.sort()
    
    ans1=sum([i[0] for i in aball[:n]])

    hen=aball[n][0]
    hen2=aball[n+1][0]
    d=set()
    t=None
    hantei=True
    for i,c,mab in aball[:n]:
        if t!=None and t!=mab:
            hantei=False
        if  c in d:
            print(ans1)
            exit()
        d.add(c)
        t=mab

    if hantei:
        print(ans1)
        exit()

    ans2=[]
    for i,c,aorb in aball[:n]:
        if aball[n][1]!=c:
            ans2+=[ans1-i+hen]
        else:
            ans2+=[ans1-i+hen2]
    print(min(ans2))
    exit()

n=int(input())
print(mincos(n,[list(map(int,input().split())) for i in range(n)]))
### BUGGY CODE ###

N = int(input())
check = tuple(i for i in range(1, N+1))
t = [[0 for i in range(9)] for _ in range(9)]
ans = 0
for i in range(1, 10):
    for l in range(1, 10):
        for q in check:
            s = 0
            if int(str(q)[0]) == i and int(str(q)[-1]) == l:
                s += 1
            t[i - 1][l - 1] += s
for i in range(1, N+1):
    if str(i)[-1] == "0":
        continue
    elif len(str(i)) == 1:
        ans += t[i-1][i-1]
    else:
        ans += t[int(str(i)[-1])-1][int(str(i)[0])-1]
print(ans)


### FIXED CODE ###

N = int(input())
t = [[0 for _ in range(9)] for l in range(9)]
ans = 0
for i in range(1, N+1):
    if len(str(i)) == 1:
        t[i-1][i-1] += 1
    elif str(i)[-1] != "0":
        t[int(str(i)[0])-1][int(str(i)[-1])-1] += 1
for i in range(1, N+1):
    a = str(i)[0]
    b = str(i)[-1]
    if b == "0":
        continue
    elif a == b:
        ans += t[(int(a)-1)][int(a)-1]
    else:
        ans += t[(int(b)-1)][int(a)-1]
print(ans)
### BUGGY CODE ###



def main():
    import sys
    input = sys.stdin.readline
    
    H,W=map(int, input().split())
    S=[]
    
    A=[[1]*(W+2)for _ in range(H+2)]
    import queue
    q=queue.Queue()
    
    for i in range(H):
        S.append(list(input()))
    
    for i in range(H):
        for j in range(W):
            if S[i][j]=="#":
                q.put((i+1,j+1,0))
            else:
                A[i+1][j+1]=0  
    
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    
    while not q.empty():
        ii,jj,d=q.get()
        for k in range(4):
            nxt_i=ii+dx[k]
            nxt_j=jj+dy[k]
            if A[nxt_i][nxt_j]==0:
                q.put((nxt_i,nxt_j,d+1))
                A[nxt_i][nxt_j]=1
    print(d)

                
    
            

main()


### FIXED CODE ###

from collections import deque

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]

black_idx = []
for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            black_idx.append([h,w])

distance = [[float("inf")]*W for _ in range(H)]
for b in black_idx:
    distance[b[0]][b[1]] = 0

d = deque(black_idx)
ans = 0

while d:
    y, x = d.popleft()
    for new_y, new_x in ([y+1, x], [y, x+1], [y-1, x], [y, x-1]):
        if 0 <= new_y <= H-1 and 0 <= new_x <= W-1 and distance[y][x] + 1 < distance[new_y][new_x]:
            distance[new_y][new_x] = distance[y][x] + 1
            ans = max(ans, distance[y][x] + 1)
            d.append([new_y, new_x])

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

def main():
    import sys
    input = sys.stdin.buffer.readline
    from collections import deque
    N, Q = (int(i) for i in input().split())
    G = [[] for _ in range(N)]
    cost = [0]*N
    for _ in range(N-1):
        a, b = (int(i) for i in input().split())
        G[a-1].append(b-1)
        G[b-1].append(a-1)
    for _ in range(Q):
        p, x = (int(i) for i in input().split())
        cost[p-1] += x
    G_color = ['WHITE' for _ in range(len(G))]
    G_parent = ['NIL' for _ in range(len(G))]

    def bfs(s):
        G_color[s] = 'GRAY'
        Q = deque()
        Q.append(s)
        while Q:
            u = Q.popleft()
            for v in G[u]:
                if G_color[v] == 'WHITE':
                    G_color[v] = 'GRAY'
                    G_parent[v] = u
                    Q.append(v)
            G_color[u] = 'BLACK'

    def dfs(v):
        seen = [False] * N
        todo = []
        seen[v] = True
        todo.append(v)
        while todo:
            u = todo[-1]
            for next_v in G[u]:
                if seen[next_v] or next_v == G_parent[u]:
                    continue
                seen[next_v] = True
                todo.append(next_v)
                cost[next_v] += cost[G_parent[next_v]]
                break
            else:
                todo.pop()

    bfs(0)
    dfs(0)
    print(*cost)


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    import sys
    input = sys.stdin.buffer.readline
    from collections import deque
    N, Q = (int(i) for i in input().split())
    G = [[] for _ in range(N)]
    cost = [0]*N
    for _ in range(N-1):
        a, b = (int(i) for i in input().split())
        G[a-1].append(b-1)
        G[b-1].append(a-1)
    for _ in range(Q):
        p, x = (int(i) for i in input().split())
        cost[p-1] += x

    def bfs(s):
        G_color = ['WHITE' for _ in range(len(G))]
        G_color[s] = 'GRAY'
        Q = deque()
        Q.append(s)
        while Q:
            u = Q.popleft()
            for v in G[u]:
                if G_color[v] == 'WHITE':
                    G_color[v] = 'GRAY'
                    cost[v] += cost[u]
                    Q.append(v)
            G_color[u] = 'BLACK'

    bfs(0)
    print(*cost)


if __name__ == '__main__':
    main()
### BUGGY CODE ###

from fractions import gcd
from functools import reduce
def lcm_base(x, y):
    return (x * y) // gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def solve(N, A):
    mod = 10 ** 9 + 7
    lcm = lcm_list(A)
    ans = [lcm//a for a in A]
    return sum(ans) % mod
    

def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    ans = solve(N, A)
    print(ans)

if __name__ == "__main__":
    main()

### FIXED CODE ###

from fractions import gcd
from functools import reduce
import sys
def lcm_base(x, y):
    return (x * y) // gcd(x, y)
 
def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)
 
def solve(N, A):
    mod = 10 ** 9 + 7
    lcm = lcm_list(A)
    ans = [lcm//a for a in A]
    return sum(ans) % mod
    
 
def main():
    read = sys.stdin.buffer.read
    N,*A = map(int,read().split())
    ans = solve(N, A)
    print(ans)
 
if __name__ == "__main__":
    main()
### BUGGY CODE ###


import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def main():
    H, W = read_values()
    F = [input().strip() for _ in range(H)]
    A = [[False for _ in range(W)] for _ in range(H)]
    Q = queue.Queue()

    for h in range(H):
        for w in range(W):
            if F[h][w] == "#":
                Q.put((h, w, 0))
                A[h][w] = True

    res = V(max)
    while not Q.empty():
        h, w, d = Q.get()
        res.ud(d)

        L = [(h + 1, w), (h - 1, w), (h, w + 1), (h, w - 1)]
        for hh, ww in L:
            if 0 <= hh < H and 0 <= ww < W:
                if A[hh][ww]:
                    continue
                Q.put((hh, ww, d + 1))
                A[hh][ww] = True
    
    print(res)


if __name__ == "__main__":
    main()


### FIXED CODE ###

import sys
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def main():
    H, W = read_values()
    F = [input().strip() for _ in range(H)]
    A = [False] * (H * W)
    Q = deque() 

    for h in range(H):
        for w in range(W):
            if F[h][w] == "#":
                Q.append((h, w, 0))
                A[h * W + w] = True

    res = V(max)
    while Q:
        h, w, d = Q.popleft()
        res.ud(d)

        for hh, ww in ((h + 1, w), (h - 1, w), (h, w + 1), (h, w - 1)):
            if 0 <= hh < H and 0 <= ww < W:
                if A[hh * W + ww]:
                    continue
                Q.append((hh, ww, d + 1))
                A[hh * W + ww] = True
    
    print(res)


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

s=input()
t=input()

ans=True
for i in range(len(s)):
    if(t.count(t[i])!=s.count(s[i])):
        ans=False
        break
if(ans):
    print('Yes')
else:
    print('No')

### FIXED CODE ###

import collections

s=input()
t=input()

ans=True
sa=collections.Counter(s)
ta=collections.Counter(t)
for i in range(len(s)):
    if(ta.get(t[i])!=sa.get(s[i])):
        ans=False
        break
if(ans):
    print('Yes')
else:
    print('No')
### BUGGY CODE ###

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

a = []
a.append(N)

if N % K == 0:
    print(0)
else:
    while True:
        if abs(N - K) not in a:
            a.append(abs(N - K))
            N = abs(N - K)
        else:
            print(min(a))
            break

### FIXED CODE ###

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

a = []
a.append(N)

if N % K == 0:
    print(0)
else:
    N = N % K
    while True:
        if abs(N - K) not in a:
            a.append(abs(N - K))
            N = abs(N - K)
        else:
            print(min(a))
            break