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

from bisect import bisect_left

N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

ans = 0
MOD = 1 << 29
T = 1 << 28
for k in range(29)[::-1]:
    ans <<= 1
    As = list(map(lambda x: x % MOD, As))
    Bs = list(map(lambda x: x % MOD, Bs))
    Bs.sort()

    num1 = 0
    for A in As:
        iT = bisect_left(Bs, T - A)
        i2T = bisect_left(Bs, 2 * T - A)
        i3T = bisect_left(Bs, 3 * T - A)
        i4T = bisect_left(Bs, 4 * T - A)
        num1 += (i2T - iT) + (i4T - i3T)

    if num1 % 2 == 1:
        ans += 1

    MOD >>= 1
    T >>= 1

print(ans)


### FIXED CODE ###

from bisect import bisect_left, bisect_right

def solve():
    maxD = 30

    N = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

    As.sort()
    Bs.sort()

    ans = 0
    for d in reversed(range(maxD)):
        m1 = 1 << d

        iA = bisect_left(As, m1)
        iB = bisect_left(Bs, m1)

        num1 = N * (N-iA + N-iB)

        As = As[:iA] + [A-m1 for A in As[iA:]]
        Bs = Bs[:iB] + [B-m1 for B in Bs[iB:]]

        As.sort()
        Bs.sort(reverse=True)

        iB = 0
        for A in As:
            while iB < N and A + Bs[iB] >= m1:
                iB += 1
            num1 += iB

        Bs.reverse()

        if num1 % 2:
            ans |= m1

    print(ans)


solve()
### BUGGY CODE ###

import itertools
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))


def gutyocu():
    ans = 0
    for i, j in itertools.combinations_with_replacement(range(N), r=2):
        ans += min(A[i: j + 1])
    return ans


A = np.array(A)
pos = np.zeros(N + 1, dtype=int)
pos[0] = -1
pos[A] = np.arange(N + 1)

ans = 0
lefts = [0] * N
rights = [0] * N
for i in reversed(range(1, N + 1)):
    p = pos[i]
    if p + 1 < N and A[p + 1] > i:
        # 右とつながった
        right = rights[p + 1]
    else:
        right = p
    if p - 1 >= 0 and A[p - 1] > i:
        # 左とつながった
        left = lefts[p - 1]
    else:
        left = p
    lefts[right] = left
    rights[left] = right
    ans += (p - left + 1) * (right - p + 1) * i
print(ans)


### FIXED CODE ###

import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))


def gutyocu():
    ans = 0
    for i, j in itertools.combinations_with_replacement(range(N), r=2):
        ans += min(A[i: j + 1])
    return ans


pos = list(range(N + 1))
pos[0] = -1
for i, a in enumerate(A):
    pos[a] = i

ans = 0
lefts = list(range(N + 1))
rights = list(range(N + 1))
for i in reversed(range(1, N + 1)):
    p = pos[i]
    if p + 1 < N and A[p + 1] > i:
        # 右とつながった
        right = rights[p + 1]
    else:
        right = p
    if p - 1 >= 0 and A[p - 1] > i:
        # 左とつながった
        left = lefts[p - 1]
    else:
        left = p
    lefts[right] = left
    rights[left] = right
    ans += (abs(left - p) + 1) * (abs(right - p) + 1) * i
print(ans)
### BUGGY CODE ###

n,q = map(int,input().split())
s = list(input())
mark = [0]*(n-1)
for i in range(n-1):
  if s[i]=='A' and s[i+1]=='C':
    mark[i] = 1
for i in range(q):
  l,r = map(int,input().split())
  l -= 1
  r -= 1
  print(sum(mark[l:r]))

### FIXED CODE ###

n,q = map(int,input().split())
s = list(input())
mark = [0]*(n+1)
for i in range(2,n+1):
  if s[i-2]=='A' and s[i-1]=='C':
    mark[i] = mark[i-1]+1
  else:
    mark[i] = mark[i-1]

for i in range(q):
  l,r = map(int,input().split())
  l = l-1
  r = r-1
  print(mark[r+1]-mark[l+1])
### BUGGY CODE ###

n,m=map(int,input().split())
l=[[0] for _ in range(n+1)]
vi=[-1 for _ in range(n+1)]
vi[0]=1
for i in range (m):
  a,b=map(int,input().split())
  l[a].append(b)
  l[b].append(a)
st=[[1,0]]

while st:
  x,i=st.pop(0)
  if vi[x]==-1:
    vi[x]=i
    for k in l[x]:
      if vi[k]==-1:
        st.append([k,x])


print("Yes")
for m in vi[2:]:
  print(m)
  
  

### FIXED CODE ###

from collections import deque
n,m=map(int,input().split())
l=[[0] for _ in range(n+1)]
vi=[0 for _ in range(n+1)]
vi[1]=-1

for i in range (m):
  a,b=map(int,input().split())
  l[a].append(b)
  l[b].append(a)
st=deque([1])
e=set()
f=set()


while st:
    x=st.popleft()
    if x not in e:
      e.add(x)
      for k in l[x]:
        if k not in f:
          st.append(k)
          f.add(k)
          vi[k]=x   
  
print("Yes")
for m in vi[2:]:
  print(m)
### BUGGY CODE ###

N,Q = map(int,input().split())
S = input()
a = [list(map(int,input().split())) for i in range(Q)]

for i in range(Q):
    r = a[i]
    S1 = S[r[0]-1:r[1]]
    print(S1.count("AC"))


### FIXED CODE ###

from itertools import accumulate

N, Q = map(int,input().split())
S = list(input())
L = [0] * Q
R = [0] * Q
for i in range(Q):
    L[i], R[i] = map(int,input().split())

S = ['0'] + S
check = [0] * N

for i in range(N):
    if S[i] == 'A':
        if S[i+1] == 'C':
            check[i] = 1

check = list(accumulate(check))
for left, right in zip(L, R):
    print(check[right-1] - check[left-1])
### BUGGY CODE ###

H,N=map(int,input().split())
AB = [tuple(map(int,input().split())) for _ in range(N)]
dp = [[0]*(N+1) for _ in range(2*10**4+5)]

for j in range(1,H+1):
  for i in range(1,N+1):
    if AB[i-1][0] < j:
      dp[j][i] = dp[j - AB[i-1][0]][i] + AB[i-1][1]
    else:
      dp[j][i] = AB[i-1][1]
  MIN = min(dp[j][1:])
  dp[j][1:] = [MIN]*(len(dp[j])-1)

print(min(dp[H][1:]))

### FIXED CODE ###

H,N=map(int,input().split())
AB = [tuple(map(int,input().split())) for _ in range(N)]
dp = [[0]*(N+1) for _ in range(2*10**4+5)]

for j in range(1,H+1):
  for i in range(1,N+1):
    if AB[i-1][0] < j:
      dp[j][i] = dp[j - AB[i-1][0]][i] + AB[i-1][1]
    else:
      dp[j][i] = AB[i-1][1]
  MIN = min(dp[j][1:])
  dp[j][1:] = [MIN]*(len(dp[j])-1)

print(min(dp[H][1:]))
### BUGGY CODE ###

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

l = []

for i in range(H):
    for j in range(W):
        if B[i][j] == '#':
            l.append((i,j))

def a(l):
    n = []
    d = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    for y, x in l:
        for dy, dx in d:
            if 0 <= y + dy < H and 0 <= x + dx < W and B[y+dy][x+dx] == '.':
                B[y+dy][x+dx] = '#'
                n.append((y+dy, x+dx))
    return set(n)

ans = 0
while len(l) != 0:
    l = a(l)
    ans += 1
print(ans-1)

### FIXED CODE ###

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

l = []

for i in range(H):
    for j in range(W):
        if B[i][j] == '#':
            l.append((i,j))

def a(l):
    n = []
    d = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    for y, x in l:
        for dy, dx in d:
            ny = y + dy
            nx = x + dx
            if 0 <= ny < H and 0 <= nx < W and B[ny][nx] == '.':
                B[ny][nx] = '#'
                n.append((ny, nx))
    return set(n)

ans = 0
while len(l) != 0:
    l = a(l)
    ans += 1
print(ans-1)
### BUGGY CODE ###

N = int(input())
lst = []
for i in range(N):
    x, y = map(int, input().split())
    lst.append([x, y, i])

def prim():
    import heapq
    used = [False] * N
    edgelist = []
    for i in lst:
        heapq.heappush(edgelist, [min(abs(lst[0][0]-i[0]), abs(lst[0][1]-i[1])), i[2]])
    used[0] = True
    res = 0
    while len(edgelist) != 0:
        minedge = heapq.heappop(edgelist)
        v = minedge[1] #今回追加される頂点
        if used[v]: #使われているとき
            continue
        used[v] = True #使われたことにする
        # print (minedge[0])
        res += minedge[0]
        for i in lst:
            heapq.heappush(edgelist, [min(abs(lst[v][0]-i[0]), abs(lst[v][1]-i[1])), i[2]])
    return res

print (prim())





### FIXED CODE ###

class UnionFind(object):
    def __init__(self, N):
        self.parent = [-1] * N #parent[i] は(i+1)番目の要素が含まれる要素数の(-1)倍
    #要素が負-->その数を親とするグループに属する要素の数×(-1)
    #要素が正-->親のindex

    #Aがどのグループに属しているかを調べる
    def root(self, A):
        if self.parent[A-1] < 0: #負の数-->その数は親
            return A
        self.parent[A-1] = self.root(self.parent[A-1]) #負では無い時、親の場所が入っているから、親が属しているグループを、自分自身に入れる(一度確認したところは直接親を見れるようにする)
        return self.parent[A-1] #親の位置を返す

    #Aが含まれているグループに属している数を返す
    def size(self, A):
        return -1 * self.parent[self.root(A)-1]

    #AとBをくっ付ける
    def connect(self, A, B):
        A = self.root(A) #Aを含むグループの親を返す
        B = self.root(B) #Bを含むグループの親を返す
        if A == B: #親が同じならなにもしない
            return False
        
        #大きい方(A)に小さい方(B)をつなぎたい
        if self.size(A) < self.size(B): #大小関係が逆の時は入れ替える
            A, B = B, A
        
        self.parent[A-1] += self.parent[B-1] #大きい方に小さい方を加える　（負の数＋負の数 = 新しいグループに含まれる数×(-1)）
        self.parent[B-1] = A #加えられた親の値を、加えた先の親の位置に書き変える
        return True
    
    def same(self, A, B):
        if self.root(A) == self.root(B):
            return True
        else:
            return False

N = int(input())

lst = []
for i in range(N):
    x, y = map(int, input().split())
    lst.append([x, y, i])
# print (lst)
edge = []
lst.sort(key = lambda x: x[0])
for i in range(1, N):
    edge.append([lst[i-1][2], lst[i][2], min(abs(lst[i-1][0]-lst[i][0]), abs(lst[i-1][1]-lst[i][1]))])
# print (edge)
lst.sort(key = lambda x: x[1])
for i in range(1, N):
    edge.append([lst[i-1][2], lst[i][2], min(abs(lst[i-1][0]-lst[i][0]), abs(lst[i-1][1]-lst[i][1]))])
# print (edge)
edge.sort(key = lambda x: x[2])

uni = UnionFind(N)
ans = 0
for i in edge:
    if not uni.same(i[0], i[1]): #違うグループのとき
        uni.connect(i[0], i[1])
        ans += i[2]
print (ans)
### BUGGY CODE ###

S = list(input())
N = int(input())

Q = [list(input().split()) for _ in range(N)]

count = 0
c = 0
for q in Q:
    if q[0] == "1":
        count += 1
        c = count % 2
    else:
        if (c == 0 and q[1] == "1") or (c == 1 and q[1] == "2"):
            S.insert(0, q[2])
        elif (c == 0 and q[1] == "2") or (c == 1 and q[1] == "1"):
            S.append(q[2])

if count % 2  == 1:
    print("".join(S[::-1]))
else:
    print("".join(S))

### FIXED CODE ###

S = list(input())
N = int(input())

Q = [list(input().split()) for _ in range(N)]

count = 0
c = 0

ans_append = []
ans_insert = []

for q in Q:
    if q[0] == "1":
        count += 1
        c = count % 2
    else:
        if (c == 0 and q[1] == "1") or (c == 1 and q[1] == "2"):
            ans_insert.append(q[2])
        elif (c == 0 and q[1] == "2") or (c == 1 and q[1] == "1"):
            ans_append.append(q[2])


if count % 2  == 1:
    ans = ans_append[::-1] + S[::-1] + ans_insert
    print("".join(ans))
else:
    ans = ans_insert[::-1] + S + ans_append
    print("".join(ans))
### BUGGY CODE ###

def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


A, B = map(int, input().split())
C = gcd(A, B)

ans = []
i = 2
while True:
    if C == 1:
        break
    if C % i != 0:
        i += 1
        continue
    ans.append(i)
    C //= i
print(len(set(ans)) + 1)


### FIXED CODE ###

def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


A, B = map(int, input().split())
C = gcd(A, B)

ans = []
while C % 2 == 0:
    ans.append(2)
    C //= 2
i = 3
while C >= i * i:
    if C % i == 0:
        ans.append(i)
        C //= i
    else:
        i += 2
if C != 1:
    ans.append(C)
print(len(set(ans)) + 1)
### BUGGY CODE ###

from fractions import gcd


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

G = []

for x in range(N):
    B = A[:x] + A[x+1:]
    g = B[0]
    for i in range(1, len(B)):
        g = gcd(g, B[i])
    G.append(g)

print(max(G))


### FIXED CODE ###

from fractions import gcd


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

G_FW    = [None] * N
G_FW[0] = A[0]

for i in range(1, N):
    G_FW[i] = gcd(G_FW[i-1], A[i])

G_RV     = [None] * N
G_RV[-1] = A[-1]

for i in reversed(range(0, N-1)):
    G_RV[i] = gcd(G_RV[i+1], A[i])

G     = [None] * N
G[ 0] = G_RV[1]
G[-1] = G_FW[-2]

for i in range(1, N-1):
    G[i] = gcd(G_FW[i-1], G_RV[i+1])

print(max(G))
### BUGGY CODE ###

def hersCode(L, n):
  ok = len(L)
  ng = -1
  while abs(ok - ng) > 1:
    mid = abs(ok + ng) // 2
    if L[mid] >= n:
      ok = mid
    else:
      ng = mid
  return ok

def main():
  N, Q = list(map(int, input().split()))
  STX = [list(map(int, input().split())) for _ in range(N)]
  D = [int(input()) for _ in range(Q)]

  STX.sort(key = lambda x: x[2])

  L = [[-1, -1]] * Q
  for s, t, x in STX:
    l = hersCode(D, s - x)
    r = hersCode(D, t - x)

    while l < r:
      if L[l][1] == -1:
        L[l] = [x, r]
        l += 1
      else:
        l = L[l][1]

  for i in L:
    print(i[0])

main()


### FIXED CODE ###

import heapq
def main():
  N, Q = list(map(int, input().split()))
  STX = tuple(list(map(int, input().split())) for _ in range(N))
  D = tuple([int(input()) for _ in range(Q)])

  L = []
  for s, t, x in STX:
    L.append((x, s - x, t - x))

  L.sort(key = lambda x: x[1])
  q = []
  cnt = 0
  push = heapq.heappush
  pop = heapq.heappop
  t = []
  for i in range(Q):
    while cnt < len(L) and D[i] >= L[cnt][1]:
      if t != []:
        push(q, t)
        t = []
      push(q, L[cnt])
      cnt += 1
    if t != [] and t[2] > D[i]:
      print(t[0])
    else:
      while q != []:
        t = pop(q)
        if t[2] > D[i]:
          print(t[0])
          break
      else:
        t = []
        print(-1)

main()
### BUGGY CODE ###

S = input()
T = input()

dp = [[[] for _ in range(len(S)+1)] for _ in range(len(T)+1)]
dp[0] = [[''] for _ in range(len(T)+1)]

for i in range(1,len(T)+1):
    for j in range(len(S)+1):
        if j == 0:
            dp[i][j] = ['']
            continue
        if S[j-1] == T[i-1]:
            dp[i][j] = [p + S[j-1] for p in dp[i-1][j-1]]
        else:
            if len(dp[i][j-1][0]) > len(dp[i-1][j][0]):
                dp[i][j] = dp[i][j-1]
            else:
                dp[i][j] = dp[i-1][j]

print(dp[-1][-1][0])


### FIXED CODE ###

S = input()
T = input()

# dp = [[[] for _ in range(len(S)+1)] for _ in range(len(T)+1)]
cdp = [[None for _ in range(len(S)+1)] for _ in range(len(T)+1)]
ddp = [[None for _ in range(len(S)+1)] for _ in range(len(T)+1)]
# dp[0] = [[''] for _ in range(len(S)+1)]
cdp[0] = [0 for _ in range(len(S)+1)]
ddp[0] = [0 for _ in range(len(S)+1)]

for i in range(1,len(T)+1):
    for j in range(len(S)+1):
        if j == 0:
            # dp[i][j] = ['']
            cdp[i][j] = 0
            ddp[i][j] = 0
            continue
        if S[j-1] == T[i-1]:
            # dp[i][j] = [p + S[j-1] for p in dp[i-1][j-1]]
            cdp[i][j] = cdp[i-1][j-1] + 1
            ddp[i][j] = 2
        else:
            if cdp[i][j-1] > cdp[i-1][j]:
                # dp[i][j] = dp[i][j-1]
                cdp[i][j] = cdp[i][j-1]
                ddp[i][j] = 1
            else:
                # dp[i][j] = dp[i-1][j]
                cdp[i][j] = cdp[i-1][j]
                ddp[i][j] = 0

# print(dp[-1][-1][0])

i, j = len(T), len(S)
L = ''
while i > 0 and j > 0:
    if ddp[i][j] == 2:
        i -= 1
        j -= 1
        L += T[i]
    elif ddp[i][j] == 1:
        j -= 1
    elif ddp[i][j] == 0:
        i -= 1

print(L[::-1])
### BUGGY CODE ###

# -*- coding: utf-8 -*-

def input_one_number():
    return int(input())

def input_multiple_number():
    return map(int, input().split())

def input_multiple_number_as_list():
    return list(map(int, input().split()))

N,M = input_multiple_number()

shilve = [-1]*(N+1)
michi = [[] for _i in range(N+1)]

for i in range(M):
	a,b = input_multiple_number()
	michi[a].append(b)
	michi[b].append(a)

que = [1]

while len(que) > 0:
	now_heya = que.pop(0)
	for nexthop in michi[now_heya]:
		if shilve[nexthop] == -1:
			shilve[nexthop] = now_heya
			que.append(nexthop)


print("Yes")
for i in shilve[2:]:
	print(i)








### FIXED CODE ###

# -*- coding: utf-8 -*-

def input_one_number():
    return int(input())

def input_multiple_number():
    return map(int, input().split())

def input_multiple_number_as_list():
    return list(map(int, input().split()))

N,M = input_multiple_number()

shilve = [-1]*(N+1)
michi = [[] for _i in range(N+1)]

for i in range(M):
	a,b = input_multiple_number()
	michi[a].append(b)
	michi[b].append(a)

que = [1]

while len(que) > 0:
	now_heya = que.pop(0)
	for nexthop in michi[now_heya]:
		if shilve[nexthop] == -1:
			shilve[nexthop] = now_heya
			que.append(nexthop)


print("Yes")
for i in shilve[2:]:
	print(i)
### BUGGY CODE ###

def pow_k(x, n):
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K = K * x
            x = x ** 2
            n = (n - 1) // 2
        else:
            x = x ** 2
            n = n // 2

    return K * x


MOD = 10**9 + 7

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

res = 0
for d in range(60):
    n0, n1 = 0, 0
    for i in range(N):
        if (An[i] >> d) & 1:
            n1 += 1
        else:
            n0 += 1

    temp = pow_k(2, d) % MOD
    temp *= (n0*n1) % MOD
    res = (res + temp) % MOD

print(res)


### FIXED CODE ###

def pow_k(x, n):
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K = K * x
            x = x ** 2
            n = (n - 1) // 2
        else:
            x = x ** 2
            n = n // 2

    return K * x


MOD = 10**9 + 7

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

res = 0
for d in range(60):
    n0, n1 = 0, 0
    for i in range(N):
        if (An[i] >> d) & 1:
            n1 += 1
        else:
            n0 += 1

    temp = pow_k(2, d) % MOD
    temp *= (n0*n1) % MOD
    res = (res + temp) % MOD

print(res)
### BUGGY CODE ###

def z_algorithm(s):
    n = len(s)
    prefix = [0] * n
    j = 0
    ret = 0
    for i in range(1, n):
        if i + prefix[i - j] < j + prefix[j]:
            prefix[i] = prefix[i - j]
        else:
            k = max(0, j + prefix[j] - i)
            while i + k < n and s[k] == s[i + k]:
                k += 1
            prefix[i] = k
            ret = max(ret, min(i, k))
            j = i
    return ret


n = int(input())
s = input()
print(max(z_algorithm(s[i:]) for i in range(n - 1)))


### FIXED CODE ###

def z_algorithm(s):
    n = len(s)
    prefix = [0] * n
    j = 0
    ret = 0
    for i in range(1, n):
        if i + prefix[i - j] < j + prefix[j]:
            prefix[i] = prefix[i - j]
        else:
            k = max(0, j + prefix[j] - i)
            while i + k < n and s[k] == s[i + k]:
                k += 1
            prefix[i] = k
            ret = max(ret, min(i, k))
            j = i
    return ret


n = int(input())
s = input()
print(max(z_algorithm(s[i:]) for i in range(n - 1)))
### BUGGY CODE ###

# -*- coding: utf-8 -*-
from operator import mul
from functools import reduce

def combination(n, r):
    r = min(n-r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n-r, -1))
    under = reduce(mul, range(1, r+1))
    return over // under

n = int(input())
s = [''.join(sorted(input())) for _ in range(n)]
ans = 0
for anagram in set(s):
    count = s.count(anagram)
    if count > 1:
        ans += combination(count, 2)
print(ans)


### FIXED CODE ###

# -*- coding: utf-8 -*-
from operator import mul
from functools import reduce

def combination(n, r):
    if n < r:
        return 0
    else:
        r = min(n-r, r)
        if r == 0:
            return 1
        else:
            over = reduce(mul, range(n, n-r, -1))
            under = reduce(mul, range(1, r+1))
            return over // under

n = int(input())
s = {}
for i in range(n):
    str = ''.join(sorted(input()))
    if str in s.keys():
        s[str] += 1
    else:
        s[str] = 1
ans = 0
for i in s:
    ans += combination(s[i], 2)
print(ans)
### BUGGY CODE ###

import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, k = map(int, input().split())
    r = n
    nums = set()
    while n not in nums:
        r = min(r, n)
        nums.add(n)
        n = abs(n - k)
    print(r)


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    n, m = map(int, input().split())
    n = abs(n - (n // m) * m)
    n = min(n, abs(n - m))
    print(n)

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

from collections import deque
import sys
input=sys.stdin.readline


def solve():
    H, W = map(int, input().split())
    m = [[H*W]*W for _ in range(H)]
    candidate = [(i, j, 0) for i in range(H) for j, s in enumerate(input()) if s=="#"]
    q = deque(candidate)

    while q:
        y, x, cost = q.popleft()
        m[y][x] = cost

        for y1, x1 in [(y-1, x), (y+1, x), (y, x-1), (y, x+1)]:
            if not (0<=x1<W and 0<=y1<H):
                continue
            if m[y1][x1] > cost+1:
                q.append((y1, x1, cost+1))

    print(max(map(max, m)))


if __name__ == "__main__":
    solve()


### FIXED CODE ###

from collections import deque


def solve():
    H, W = map(int, input().split())
    m = [[-1]*W for _ in range(H)]
    l = [list(input()) for _ in range(H)]
    dx = [-1, 1, 0, 0]
    dy = [0, 0, -1, 1]
    q = deque([])

    for i in range(H):
        for j in range(W):
            if l[i][j]=="#":
                q.append((i, j))
                m[i][j] = 0
    
    while q:
        y, x = q.popleft()

        for px, py in zip(dx, dy):
            x1, y1 = x + px, y + py
            if not (0<=x1<W and 0<=y1<H):
                continue
            if m[y1][x1] == -1:
                q.append((y1, x1))
                m[y1][x1] = m[y][x] + 1

    print(max(map(max, m)))


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

n,m,k = map(int,input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
import sys
sys.setrecursionlimit(10**6)
maxc = 0
a_i=0
b_i=0
def dfs(a_i,b_i,t,cnt):
    global maxc
    
    if(a_i==n and b_i==m):
        maxc = m+n
        return
    
    if(a_i < n and (A[a_i]+t)<=k):
        dfs(a_i+1,b_i,A[a_i]+t,cnt+1)
    else:
        maxc = max(cnt,maxc)
    if(b_i < m and (B[b_i]+t)<=k):
        dfs(a_i,b_i+1,B[b_i]+t,cnt+1)
    else:
        maxc = max(cnt,maxc)


        
dfs(0,0,0,0)
print(maxc)

### FIXED CODE ###

import bisect

n,m,k = map(int,input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

A_sum = [0]*(n+1)
B_sum = [0]*(m+1)

for i in range(n):
    A_sum[i+1] = A_sum[i] + A[i]

for i in range(m):
    B_sum[i+1] = B_sum[i] + B[i]

A_limit = bisect.bisect_right(A_sum,k) - 1
mc = 0
B_i = 0
for i in range(A_limit,-1,-1):
    remain_k = k - A_sum[i]
    while((remain_k-B_sum[B_i])>=0):
        B_i += 1
        if(B_i==(m+1)):
            break
    B_i -= 1
    mc = max(mc,(i+B_i))

print(mc)
### BUGGY CODE ###

def cmb(n, r):
	if n - r < r:
		r = n - r
	if r == 0:
		return 1
	if r == 1:
		return n
	
	num = [n - r + k + 1 for k in range(r)] #numerator(分子)
	den = [k + 1 for k in range(r)] #denominator(分母)
	
	for p in range(2, r + 1):
		pivot = den[p - 1]
		if pivot > 1:
			offset = (n - r) % p
			for k in range(p - 1, r, p):
				num[k - offset] /= pivot
				den[k] /= pivot
	
	result = 1
	for k in range(r):
		if num[k] > 1:
			result *= int(num[k])
	
	return result

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

print((2**n - 1 - cmb(n,a) - cmb(n,b)) % (10**9 + 7))

### FIXED CODE ###

def cmb(n, r):
	if n - r < r:
		r = n - r
	if r == 0:
		return 1
	if r == 1:
		return n
	
	num = [n - r + k + 1 for k in range(r)] #numerator(分子)
	den = [k + 1 for k in range(r)] #denominator(分母)
	
	for p in range(2, r + 1):
		pivot = den[p - 1]
		if pivot > 1:
			offset = (n - r) % p
			for k in range(p - 1, r, p):
				num[k - offset] /= pivot
				den[k] /= pivot
	
	result = 1
	for k in range(r):
		if num[k] > 1:
			result *= int(num[k])
			result = result % mod
	
	return result

n,a,b = map(int,input().split())
mod = 10**9+7

print((pow(2,n,mod) - 1 - cmb(n,a) - cmb(n,b)) % mod)