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

h,w,m=list(map(int, input("").split()))
hb= [[0, i+1] for i in range(h)]
wb=[[0, i+1] for i in range(w)]
bo=[]
for i in range(m):
    a,b=list(map(int, input("").split()))
    hb[a-1][0]+=1
    wb[b-1][0]+=1
    bo.append([a,b])
list.sort(hb, reverse=True)
list.sort(wb, reverse=True)
mwb=[]
mhb=[]
for i in hb:
    if i[0]==hb[0][0]:
        mhb.append(i[1])
    else:
        break
for i in wb:
    if i[0]==wb[0][0]:
        mwb.append(i[1])
    else:
        break
c=0
for h,w in bo:
    if h in mhb and w in mwb:
        c+=1
out=hb[0][0]+wb[0][0]
if c==len(mwb)*len(mhb):
    out-=1

print(out)

### FIXED CODE ###

h,w,m=list(map(int, input("").split()))
bo=[]
hb=[0]*h
wb=[0]*w
for i in range(m):
    a,b=list(map(int, input("").split()))
    #各行列に爆弾いくつあるか
    hb[a-1]+=1
    wb[b-1]+=1
    #爆弾の座標
    bo.append([a,b])
#ソート
mwb=[]
mhb=[]
#爆弾数が最大の行と列番号をメモ
hmax=max(hb)
wmax=max(wb)
for i in range(h):
    if hb[i]==hmax:
        mhb.append(i)
for i in range(w):
    if wb[i]==wmax:
        mwb.append(i)
c=0
#最大の爆弾数の行と列に含まれる爆弾数=cの確認
for h,w in bo:
     #h in mhb and w in mwb
    if(hb[h-1] == hmax and wb[w-1] == wmax):
        c+=1
#行と列の最大爆弾数の和
out=hmax+wmax
#行と列の最大数の積がcと一致するなら必ず重複があるので－1
if c==len(mwb)*len(mhb):
    out-=1
print(out)
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split()))
for i in range(1,N+1):
    print(A.count(i))

### FIXED CODE ###

N=int(input())
A=list(map(int,input().split()))
ans=[0]*N
for i in range(N-1):
    ans[A[i]-1]+=1
for i in range(N):
    print(ans[i])
### BUGGY CODE ###

n=int(input())
s=input()
st=set()
seti=set()
for i in range(n-2):
    if i in seti:
        continue
    seti.add(i)
    
    for j in range(i+1,n-1):
        for k in range(j+1,n):
            pas=str(s[i])+str(s[j])+str(s[k])
            st.add(pas)


print(len(st))

### FIXED CODE ###

n=int(input())
s=input()
ans=0
for i in range(0,10):
    a=s.find(str(i))    #findは文字列を検索して最初のインデックスを返す
    if a==-1:
        continue
    for j in range(0,10):
        b=s.find(str(j),a+1)
        if b==-1:
            continue
        for k in range(10):
            if s.find(str(k),b+1) != -1:
                ans+=1

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

s = input()
ans = 0

for i in range(len(s)):
  p = 4
  while i+p<=len(s):
    if int(s[i:i+p])%2019==0:
      ans += 1
    p += 1
print(ans)

### FIXED CODE ###

S = input()[::-1]
ans = 0
mods = [0] * 2019
mods[0] = 1
current = 0
x = 1
for s in S:
  current = (current + x * int(s)) % 2019
  ans += mods[current % 2019]
  mods[current % 2019] += 1
  x = x * 10 % 2019
print(ans)
### BUGGY CODE ###

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

for i in range(1, n+1):
    print(A.index(i) + 1, end=" ")



### FIXED CODE ###

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

ans = [0] * n

for i in range(n):
    s = A[i]
    ans[s-1] = i+1
print(*ans)
### BUGGY CODE ###

#158_D
s = input()
q = int(input())
for _ in range(q):
    a = list(input().split())
    if a[0] == "1":
        s = s[::-1]
    elif a[0] == "2":
        if a[1] == "1":
            s = a[2] + s
        elif a[1] == "2":
            s = s + a[2]
print(s)

### FIXED CODE ###

#158_D
s = input()
q = int(input())

from collections import deque

s = deque(s)

rev = 1
for _ in range(q):
    Q = list(input().split())
    if Q[0] == "1":
        rev *= -1
    elif Q[0] == "2":
        if (Q[1]=="1" and rev==1) or (Q[1]=="2" and rev==-1):
            s.appendleft(Q[2])
        elif (Q[1]=="2" and rev==1) or (Q[1]=="1" and rev==-1):
            s.append(Q[2])
            
s = "".join(s)
            
if rev == -1:
    s = s[::-1]

print(s)
### BUGGY CODE ###

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
#N=int(input())
#S=[list(map(int,input().split())) for i in range(N)]
N=int(input())
X=list(map(int,input().split()))
lastX=X.pop()
mod=10**9+7
ferumaC=10**5+1
fac=[1,1]
for i in range(2,ferumaC):
    fac.append(fac[i-1]*i%mod)
ipow=[pow(i,mod-2,mod) for i in range(ferumaC)]
count=0
for i in range(1,len(X)):
    count2=0
    for n in range(min(i,len(X)-i)):
        a=((len(X)-1)//i)*i+n
        if a>len(X)-1:
            a-=i
        #print(n,a,i)
        count2+=X[a]-X[n]
    count=(count+ipow[i+1]*ipow[i]*count2)%mod
for i in range(len(X)):
    count=(count+(lastX-X[i])*ipow[len(X)-i])%mod
count=count*fac[N-1]%mod
print(count)


### FIXED CODE ###

from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
#N=int(input())
#S=[list(map(int,input().split())) for i in range(N)]
N=int(input())
X=list(map(int,input().split()))
lastX=X.pop()
mod=10**9+7
ferumaC=10**5+1
fac=[1,1]
for i in range(2,ferumaC):
    fac.append(fac[i-1]*i%mod)
ipow=[pow(i,mod-2,mod) for i in range(ferumaC+1)]

ruiipow=[0,ipow[2]*ipow[1]%mod]

for i in range(2,ferumaC):
    ruiipow.append((ruiipow[i-1]+ipow[i+1]*ipow[i])%mod)

count=0
"""for i in range(1,len(X)):
    count2=0
    for n in range(min(i,len(X)-i)):
        a=((len(X)-1)//i)*i+n
        if a>len(X)-1:
            a-=i
        #print(n,a,i)
        count2+=X[a]-X[n]
    count=(count+ipow[i+1]*ipow[i]*count2)%mod"""
for i in range(len(X)):

    count=(count+(ruiipow[i]-ruiipow[len(X)-i-1])*X[i])%mod
    #print(i,count*fac[N-1]%mod)

for i in range(len(X)):
    count=(count+(lastX-X[i])*ipow[len(X)-i])%mod
count=count*fac[N-1]%mod
print(count)
### BUGGY CODE ###

#上下方向の探索と左右方向の探索をまとめる
#まず表を完成させる-->全マスの探索
H, W = map(int, input().split())
lst = [['#'] * (W+2)]
lst += [['#'] + list(input()) + ['#'] for i in range(H)]
lst += [['#'] * (W+2)]


lst_ue = [[0] * (W+2) for i in range(H+2)]
lst_shita = [[0] * (W+2) for i in range(H+2)]
lst_migi = [[0] * (W+2) for i in range(H+2)]
lst_hidari = [[0] * (W+2) for i in range(H+2)]

for i in range(1, H+1):
    for j in range(1, W+1):
        lst_hidari[i][j] = 0 if lst[i][j] == '#' else 1 + lst_hidari[i][j-1]
    for j in range(W, 0, -1):
        lst_migi[i][j] = 0 if lst[i][j] == '#' else 1 + lst_migi[i][j+1]

for j in range(1, W+1):
    for i in range(1, H+1):
        lst_ue[i][j] = 0 if lst[i][j] == '#' else 1 + lst_ue[i-1][j]
    for i in range(H+1, 0, -1):
        lst_shita[i][j] = 0 if lst[i][j] == '#' else 1 + lst_shita[i+1][j]

ans = 0
for i in range(1,H+1):
    for j in range(1, W+1):
        ans = max(ans, lst_ue[i][j]+lst_shita[i][j]+lst_migi[i][j]+lst_hidari[i][j]-3)

print (ans)


### FIXED CODE ###

#上下方向の探索と左右方向の探索をまとめる
#まず表を完成させる-->全マスの探索
H, W = map(int, input().split())
lst = [['#'] * (W+2)]
lst += [['#'] + list(input()) + ['#'] for i in range(H)]
lst += [['#'] * (W+2)]


lst_ue = [[0] * (W+2) for i in range(H+2)]
lst_shita = [[0] * (W+2) for i in range(H+2)]
lst_migi = [[0] * (W+2) for i in range(H+2)]
lst_hidari = [[0] * (W+2) for i in range(H+2)]

for i in range(1, H+1):
    for j in range(1, W+1):
        lst_hidari[i][j] = 0 if lst[i][j] == '#' else 1 + lst_hidari[i][j-1]
    for j in range(W, 0, -1):
        lst_migi[i][j] = 0 if lst[i][j] == '#' else 1 + lst_migi[i][j+1]

for j in range(1, W+1):
    for i in range(1, H+1):
        lst_ue[i][j] = 0 if lst[i][j] == '#' else 1 + lst_ue[i-1][j]
    for i in range(H+1, 0, -1):
        lst_shita[i][j] = 0 if lst[i][j] == '#' else 1 + lst_shita[i+1][j]

ans = 0
for i in range(1,H+1):
    for j in range(1, W+1):
        ans = max(ans, lst_ue[i][j]+lst_shita[i][j]+lst_migi[i][j]+lst_hidari[i][j]-3)

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

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

turns_num_list = list()

for i in range(len(S)):

  left = [s == "W" for s in S[: i]].count(True)
  right = [s == "E" for s in S[i+1: ]].count(True)

  turns_num_list.append(left+right)

print(min(turns_num_list))

### FIXED CODE ###

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

length = len(S)
min_num = 300000
left = 0
right = S.count("E")

for i in range(length):

  if i > 0:
    left += int(S[i - 1] == "W")

  if i < length:
    right -= int(S[i] == "E")

  if left + right < min_num:
    min_num = left + right

print(min_num)
### BUGGY CODE ###

S = input()
strs = list(S)

m = {}
l_s = len(strs)

for i in range(l_s+1):
    m[i] = {}
    for j in range(4):
        m[i][j] = 0

m[l_s][3] = 1

for i in range(l_s)[::-1]:
    s = strs[i]
    for j in range(4):
        m[i][j] = m[i+1][j]
    if s == 'A':
        m[i][0] += m[i+1][1]
    elif s == 'B':
        m[i][1] += m[i+1][2]
    elif s == 'C':
        m[i][2] += m[i+1][3]
    else:
        m[i][0] = m[i+1][0] * 3 + m[i+1][1]
        m[i][1] = m[i+1][1] * 3 + m[i+1][2]
        m[i][2] = m[i+1][2] * 3 + m[i+1][3]
        m[i][3] *= 3
mod = 10**9+7
print(m[0][0] % mod)


### FIXED CODE ###

S = input()
strs = list(S)

m = {}
l_s = len(strs)

for i in range(l_s+1):
    m[i] = {}
    for j in range(4):
        m[i][j] = 0

m[l_s][3] = 1

mod = 10**9+7
for i in range(l_s)[::-1]:
    s = strs[i]
    for j in range(4):
        m[i][j] = m[i+1][j]
    if s == 'A':
        m[i][0] += m[i+1][1]
    elif s == 'B':
        m[i][1] += m[i+1][2]
    elif s == 'C':
        m[i][2] += m[i+1][3]
    else:
        m[i][0] = m[i+1][0] * 3 + m[i+1][1]
        m[i][1] = m[i+1][1] * 3 + m[i+1][2]
        m[i][2] = m[i+1][2] * 3 + m[i+1][3]
        m[i][3] *= 3
    for j in range(4):
        m[i][j] = m[i][j] % mod
print(m[0][0] % mod)
### BUGGY CODE ###

from collections import deque
import copy
h,w = map(int, input().split())
field = [[j == "#" for j in input()] for _ in range(h)]
que = deque([(i, j) for i in range(h) for j in range(w) if field[i][j]])

count = 0
while True:
    count += 1
    nque = deque()
    while que:
        y,x = que.popleft()
        if x >0 and not field[y][x-1]:
            field[y][x-1] = True
            nque.append([y,x-1])
        if x <w-1 and not field[y][x+1]:
            field[y][x+1] = True
            nque.append([y,x+1])
        if y >0 and not field[y-1][x]:
            field[y-1][x] = True
            nque.append([y-1,x])
        if y < h-1 and not field[y+1][x]:
            field[y+1][x] = True
            nque.append([y+1,x])
    if nque:
        que = copy.copy(nque)
    else:
        break
print(count-1)
    

### FIXED CODE ###

from collections import deque
import copy

def main():
    h, w = map(int, input().split())
    a = [[j == "#" for j in input()] for _ in range(h)]


    # スタート地点をqueueに追加
    que = deque([(i, j) for i in range(h) for j in range(w) if a[i][j]])

    count = 0
    while True:
        count += 1
        nque = deque()
        while que:
            y, x = que.popleft()
            if x > 0 and not a[y][x-1]:
                a[y][x-1] = True
                nque.append((y,x-1))
            if x < w-1 and not a[y][x+1]:
                a[y][x+1] = True
                nque.append((y,x+1))
            if y > 0 and not a[y-1][x]:
                a[y-1][x] = True
                nque.append((y-1,x))
            if y < h-1 and not a[y+1][x]:
                a[y+1][x] = True
                nque.append((y+1,x))

        if nque:
            que = copy.copy(nque)
        else:
            break

    print(count - 1)

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

r,c,k = map(int,input().split())
dp = [[[0 for _ in range(4)] for _ in range(c+1)] for _ in range(r+1)]
g = [[0]*(c+1) for _ in range(r+1)]
for _ in range(k):
    a,b,v = map(int,input().split())
    g[a][b] = v

for i in range(r+1):
    for j in range(c+1):
        for k in range(4):
            here = dp[i][j][k]
            if i + 1 <= r:
                dp[i + 1][j][0] = max(dp[i + 1][j][0], here)
                dp[i + 1][j][1] = max(dp[i + 1][j][1], here + g[i + 1][j])
            if j + 1 <= c:
                dp[i][j + 1][k] = max(dp[i][j + 1][k], here)
                if k < 3:
                    dp[i][j + 1][k+1] = max(dp[i][j + 1][k+1], here + g[i][j + 1])
# ans = 0
            # tmp  =dp[i][j][m]
            # if i+1<=r:
            #     dp[i+1][j][0] = max(tmp,dp[i+1][j][0])
            #     dp[i+1][j][1] = max(dp[i+1][j][1],tmp + g[i+1][j])
            # if j+1<=c:
            #     dp[i][j+1][m] = max(dp[i][j+1][m],tmp)
            #     if m<3:
            #         dp[i][j+1][m+1] = max(dp[i][j+1][m+1],tmp + g[i][j+1]) 
print(max(dp[-1][-1]))
 

### FIXED CODE ###

import sys

R, C, K = map(int, input().split())
item = [[0] * (C + 1) for _ in range(R + 1)]  # dp配列と合わせるために, 0行目, 0列目を追加している.
for s in sys.stdin.readlines():
    r, c, v = map(int, s.split())
    item[r][c] = v

dp = [[[0] * (C + 1) for _ in range(R + 1)] for _ in range(4)]
for i in range(R + 1):
    for j in range(C + 1):
        for k in range(4):
            here = dp[k][i][j]
            if i + 1 <= R:
                dp[0][i + 1][j] = max(dp[0][i + 1][j], here)
                dp[1][i + 1][j] = max(dp[1][i + 1][j], here + item[i + 1][j])
            if j + 1 <= C:
                dp[k][i][j + 1] = max(dp[k][i][j + 1], here)
                if k < 3:
                    dp[k + 1][i][j + 1] = max(dp[k + 1][i][j + 1], here + item[i][j + 1])
ans = 0
for k in range(4):
    ans = max(ans, dp[k][-1][-1])
print(ans)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
q = int(input())
ql = []
R = 0
for _ in range(q):
    l,r = map(int, input().split())
    ql.append((l,r))
    R = max(R,r)
R += 1

is_prime = [True]*(R+1)
is_prime[0] = False
is_prime[1] = False
for n in range(2,R+1):
    if is_prime[n]:
        x = 2
        while n*x<R+1:
            is_prime[n*x] = False
            x += 1
primes = []
for i in range(len(is_prime)):
    if is_prime[i]:
        primes.append(i)

like2017 = [0]*(R+1)
for i in range(R):
    if i%2==1 and (i in primes) and ((i+1)//2 in primes):
        like2017[i+1] = like2017[i] + 1
    else:
        like2017[i+1] = like2017[i]

for (l,r) in ql:
    print(like2017[r+1] - like2017[l])


### FIXED CODE ###

def sieve(n):
    res = list(range(n+10)) # nが小さい時よう
    res[0] = -1
    res[1] = -1
    i = 2
    while i*i<n:
        if res[i]<i:
            i += 1
            continue
        j = i*i
        while j<n:
            if res[j]==j:
                res[j] = i
            j += i
        i += 1
    return res

s = sieve(10**5+10)
acc = [0]*(10**5+10)
for i in range(2,10**5+9):
    if i%2==1 and s[i]==i and s[(i+1)//2]==(i+1)//2:
        acc[i+1] = acc[i]+1
    else:
        acc[i+1] = acc[i]

q = int(input())
res = [0]*q
for i in range(q):
    l,r = map(int, input().split())
    res[i] = acc[r+1]-acc[l]

for r in res:
    print(r)
### BUGGY CODE ###

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

for _ in range(n):
    i = a.index(min(a))
    print(i + 1, end = " ")
    a[i] = n + 1

### FIXED CODE ###

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

l = sorted(range(len(a)), key=a.__getitem__)
for i in l:
    print(i + 1, end = " ")
### BUGGY CODE ###

if __name__ == '__main__':
    n = int(input().strip())
    A = list(map(int, input().strip().split()))
    A.sort(reverse=True)
    score = 0
    if not len(A) < 2:
        min_max_list = [0] * n
        min_max_dict = {A[1]: 2}
        max_value = A[1]
        score = A[0]
        value_list = [A[1]]
        for num, a in enumerate(A[2:]):
            score += max_value
            min_max_dict[max_value] -= 1
            if a in min_max_dict:
                min_max_dict[a] += 2
            else:
                min_max_dict[a] = 2
                value_list.append(a)
            if min_max_dict[max_value] == 0:
                del(value_list[0])
                max_value = value_list[0]
    print(score)


### FIXED CODE ###

if __name__ == '__main__':
    n = int(input().strip())
    A = list(map(int, input().strip().split()))
    A.sort(reverse=True)

    score = A[0] + sum([A[1+int(i/2)]for i in range(0, n-2)])

    print(score)
### BUGGY CODE ###

# ABC134-E

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l1=[I() for _ in range(n)]

  l1.reverse()
  l2=[0]*n

  c=1

  # print(l1)
  for i,x in enumerate(l1):
    # print(l2)
    if l2[i]==0:
      l2[i]=i+1
    for j in range(i,n):
      y=l1[j]

      if c==n-1:
        return max(l2)

      if x>y and l2[j]==0:
        c+=1
        l2[j]=i+1
        x=y

      if l1[j]==0:
        break

  return max(l2)

# main()
print(main())


### FIXED CODE ###

import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  _l=[I() for _ in range(n)]
  l=collections.deque()

  for A in _l:
    if len(l)==0:
      l.append(A)
      continue

    lv=l[0]
    rv=l[-1]
    if rv<A:
      l[-1]=A
      continue
    if A<=lv:
      l.appendleft(A)
      continue

    li=0
    ri=len(l)-1
    while ri-li>1:
      mi=(ri+li)//2
      if l[mi]<A:
        li=mi
      else:
        ri=mi

    l[li]=A

  # print(l)
  return len(l)

# main()
print(main())
### BUGGY CODE ###


import math




N=int(input())

anslist=[]
for i in range(1,N+1):
    ans=0
    if i<=5:
        anslist.append(ans)
    else:
        ma=int(math.sqrt(i/2))+1
        for x in range(1,ma):
            for y in range(x,ma):
                for z in range(y,ma):
                    if (x+y+z)**2<i:
                        continue
                    if x**2+y**2+z**2+x*y+y*z+z*x==i:
                        if x==y and y==z:
                            ans+=1
                        elif x==y or y==z or z==x:
                            ans+=3
                        else:
                            ans+=6
                    if 6*(x**2)>i:
                        break
        anslist.append(ans)
            

for i in range(N):
    print(anslist[i])

### FIXED CODE ###

import math




N=int(input())

anslist=[0]*(N+1)
ma=int(math.sqrt(N))+1
for x in range(1,ma):
    for y in range(x,ma):
        for z in range(y,ma):
            n=x**2+y**2+z**2+x*y+y*z+z*x
            if n <=N:
                if x==y and y==z:
                    anslist[n]+=1
                elif x==y or y==z or z==x:
                    anslist[n]+=3
                else:
                    anslist[n]+=6

            
for i in range(1,N+1):
    print(anslist[i])
### BUGGY CODE ###

s = list(map(int,input().split()))
N = s[0]
K = s[1]
Q = s[2]
A = [int(input()) for i in range(Q)]

for i in range(1,N+1):
    if K-Q+A.count(i)<=0:
        print("No")
    else:
        print("Yes")


### FIXED CODE ###

N,K,Q = map(int,input().split())
A = [int(input()) for i in range(Q) ]
point = [0]*(N+1)
for i in range(Q):
    point[A[i]] += 1
for i in range(N):
    if K-Q+point[i+1]<=0:
        print("No")
    else:
        print("Yes")
### BUGGY CODE ###

X, K, D = map(int, input().split())
if X < 0:
    X = -X
step = 0
while X > D and not K == 0:
    step += 1
    X -= D
    K -= 1
if K == 0:
    print(X)
    exit()
if K % 2 == 0:
    print(X)
else:
    print(abs(X-D))


### FIXED CODE ###

X, K, D = map(int, input().split())
if X < 0:
    X = -X
step = X//D
X -= D*step
K -= step

if K <= 0:
    print(X-K*D)
    exit()
if K % 2 == 0:
    print(X)
else:
    print(abs(X-D))
### BUGGY CODE ###

from collections import deque
h, w = map(int, input().split())
que = deque()

blck = [1] * (w + 2)
data = [blck]
for ln in range(1, h+1):
    line = list(input().rstrip())
    lineN = [0] * (w+2)
    lineN[0] = 1
    lineN[-1] = 1
    for r, letter in zip(range(1, w+1), line):
        if letter == '#':
            lineN[r] = 1
            que.append((ln, r))
    data.append(lineN)
data.append(blck)


count = 0
next = deque()
while len(que) > 0:
    count += 1
    while len(que) > 0:
        row, col = que.popleft()
        for pos in ((row - 1, col), (row + 1, col), (row, col - 1), (row, col + 1)):
            r, c = pos
            if data[r][c] == 0:
                next.append(pos)
                data[r][c] = 1
    que, next = next, que

print(count - 1)

### FIXED CODE ###

from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)
### BUGGY CODE ###

from collections import Counter

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
s = Counter(A)
t=[0]*(10**5+1)
for i in range(10**5+1):
    t[i] = s[i]*i
    
for i in range(Q):
    B, C = map(int, input().split())
    num = t[B]//(B)
    t[B] = 0
    t[C] += num*(C)
    print(sum(t))


### FIXED CODE ###

from collections import Counter

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
s = Counter(A)
t=[0]*(10**5+1)
for i in range(10**5+1):
    t[i] = s[i]*i

sumnum=sum(t)
for i in range(Q):
    B, C = map(int, input().split())
    num = t[B]//(B)
    t[B] = 0
    t[C] += num*(C)
    sumnum=sumnum-num*B+num*C
    print(sumnum)
### BUGGY CODE ###

#!/usr/bin/env python
# -*- coding: utf-8 -*-

A, B  = map(int, raw_input().split())

while True:
	if A == 0 and B == 0:
		print("Brown")
		break
	
	while A > 1:
		A -= 2
		B += 1
	
	while B > 1:
		B -= 2
		A += 1
	
	if A == 1 and B == 0:
		print("Brown")
		break
	elif A == 0 and B == 1:
		print("Alice")
		break



### FIXED CODE ###

#!/usr/bin/env python
# -*- coding: utf-8 -*-

A, B  = map(int, raw_input().split())

if abs(A - B) < 2:
	print("Brown")
else:
	print("Alice")
### BUGGY CODE ###

import sys
def input(): return sys.stdin.readline().strip()
n=int(input())
aa=list(map(int,input().split()))
m=n-1
while m>=0:
    if (m==0 and aa.count(m)!=1) or (m>0 and aa.count(m)!=2):
        print(0)
        sys.exit()
    m-=2
print(2**(n//2))

### FIXED CODE ###

import sys
def input(): return sys.stdin.readline().strip()
n=int(input())
aa=sorted(map(int,input().split()),reverse=True)
m=n-1
for i in range(n//2):
    if aa[2*i]!=m or aa[2*i+1]!=m:
        print(0)
        sys.exit()
    m-=2
print((2**(n//2))%(10**9+7))
### BUGGY CODE ###

s=input()
count_1=0
l=[]
num =int(input())
for i in range(num):
    q=input().rstrip().split()
    l.append(q)

for i in l:
    if i[0]=="1":
        s=s[::-1]
        
    elif i[1]=="1":
    
        s=i[2]+s
    elif i[1]=="2":
        s=s+i[2]
print(s)

### FIXED CODE ###

s=input()
s1=""
s2=""
count_1=0
l=[]
num =int(input())
 
 
for x in range(num):
    i=input().rstrip().split()
    if i[0]=="1":
        count_1+=1
        
    elif i[1]=="1" and count_1%2==0:
        s1=i[2]+s1
   
    elif i[1]=="1" and count_1%2==1:
        s2=s2+i[2]
 
    elif i[1]=="2" and count_1%2==0:
        s2=s2+i[2]
 
    elif i[1]=="2" and count_1%2==1:
        s1=i[2]+s1
s=s1+s+s2
 
if count_1%2==1:
    s=s[::-1]
print(s)