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

from collections import deque

def bfs(maze, visited, sy, sx, gy, gx):
  queue = deque([[sy, sx]])
  visited[sy][sx] = 0
  while queue:
    y,x = queue.popleft()
    if [y, x] == [gy, gx]:
      return visited[y][x]
    for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]):
      new_y, new_x = y+j, x+k
      if new_y < 0 or new_y >= h or new_x < 0 or new_x >= w :
        continue
      if maze[new_y][new_x] == "." and visited[new_y][new_x] == -1:
        visited[new_y][new_x] = visited[y][x] + 1
        queue.append([new_y, new_x])

h,w = map(int,input().split())
maze = [input() for i in range(h)]

ans = []
for i in range(0,h) :
  for j in range(0,w):
    for k in range(0,h) :
      for l in range(0,w) :
        sy = i ; sx = j
        gy = k ; gx = l
        if maze[sy][sx] == '#' or maze[gy][gx] == '#' :
          continue
        visited = [[-1]*w for _ in range(h)]
        ans.append(bfs(maze, visited, sy, sx, gy, gx))

print(max(ans))

### FIXED CODE ###

from collections import deque

def bfs(maze, visited, sy, sx):
  queue = deque([[sy, sx]])
  visited[sy][sx] = 0
  va = 0
  while queue:
    y,x = queue.popleft()
    if visited[y][x] > va :
      va = visited[y][x]
    for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]):
      new_y, new_x = y+j, x+k
      if maze[new_y][new_x] == "." and visited[new_y][new_x] == -1:
        visited[new_y][new_x] = visited[y][x] + 1
        queue.append([new_y, new_x])
  return va

h,w = map(int,input().split())
wall = ''
for i in range(w+2):
  wall += '#'
maze = [wall]
for i in range(h) :
  m1 = input()
  maze.append('#'+m1+'#')
maze .append(wall)

ans = []
for sy in range(1,h+1) :
  for sx in range(1+w+1):
    if maze[sy][sx] == '#' :
      continue
    visited = [[-1]*(w+2) for _ in range(h+2)]
    ans.append(bfs(maze, visited, sy, sx))

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

k = int(input())
n = 0
for i in range(1,10**6):
    n = n*10+7
    if n%k==0:
        print(i)
        exit()
print(-1)

### FIXED CODE ###

k = int(input())
n = 0
for i in range(1,10**6):
    n = (n*10+7)%k
    if n%k==0:
        print(i)
        exit()
print(-1)
### BUGGY CODE ###

from math import log,floor
N = int(input())
A = list(map(int, input().split()))

# log(10**9 + 10**9)/log(2) = 30.89 < 31
#print(log(2 * 10**9) / log(2))

A.sort(reverse=True)
usedI = [0] * N
cnt = 0

for i in range(N):
  if usedI[i] == 1:
    continue
  #print(i, log(A[i]) / log(2) )
  x = floor(log(A[i]) / log(2)) + 1 #実現可能なべき上
  search_x = 2 ** x - A[i]
  l = i+1
  r = N-1
  while l <= r:
    m = (l+r)//2
    #print(l,r,m)

    if search_x < A[m]:
      l = m if l<m else m+1
    elif search_x > A[m]:
      r = m if m<r else m-1
    else:
      if usedI[m] == 0:
        cnt += 1
        usedI[i] = 1
        usedI[m] = 1
      else:
        isfound = False
        for k in range(m+1,r+1):
          if A[k] != search_x:
              break
          if usedI[k] == 1:
              continue
          isfound = True
          break
        if not isfound:
          for k in range(m-1,l-1,-1):
            if A[k] != search_x:
              break
            if usedI[k] == 1:
              continue
            isfound = True
            break
        if isfound:
          cnt += 1
          usedI[i] = 1
          usedI[k] = 1
      break

print(cnt)

### FIXED CODE ###

from math import log,floor
from collections import Counter
N = int(input())
A = list(map(int, input().split()))

A.sort(reverse=True)
C = Counter(A)

cnt = 0
last_a = -1
for i in range(N):
  a = A[i]
  if last_a == a:
    continue
  last_a = a
  count_a = C[a]
  if count_a <= 0:
    continue
  b = pow(2,floor(log(a)/log(2))+1) - a
  count_b = C[b]
  if count_b <= 0:
    continue
  i += count_a - 1
  x = min(count_a, count_b) if a != b else count_a // 2
  cnt += x
  C[a] -= x
  C[b] -= x
  
print(cnt)
### BUGGY CODE ###

import sys 
sys.setrecursionlimit(10**7)
N, T = map(int, input().split())
A = []
for _ in range(N):
  a, b = map(int,input().split())
  A.append((a, b))
A.sort(key=lambda x: x[1], reverse=True)
A.sort(key=lambda x: x[0])
ans = []
max_point = [-1 for _ in range(T)]
def solve(point, x, unvisit_list):
  if x >= T or len(unvisit_list) == 0:
    ans.append(point)
    return 0
  elif max_point[x] >= point:
    return 0
  else:
    max_point[x] = point
    for k in range(len(unvisit_list)):
      new_unvisit = unvisit_list[:k] + unvisit_list[k+1:]
      if x + unvisit_list[k][0] >= T:
        ans.append(point+max(unvisit_list, key=lambda x: x[1])[1])
        return 0
      solve(point+unvisit_list[k][1], x + unvisit_list[k][0], new_unvisit)
    return 0

while A:
  item = A[0]
  del A[0]
  solve(item[1], item[0], A)
print(max(ans))


### FIXED CODE ###

N, T = map(int, input().split())
A = []
for _ in range(N):
  a, b = map(int,input().split())
  A.append((a, b))
A.sort(key=lambda x: x[0])

newdp = [0 for _ in range(T)]
ans = 0
from collections import deque
A = deque(A)
visited = [0]
while A:
  olddp = tuple(newdp)
  newdp = list(newdp)
  X = A.popleft()
  a = X[0]
  b = X[1]
  for v in visited:
    if v + a > T-1:
      ans = max(ans, olddp[v]+b)
    else:

      if newdp[v+a] == 0:
        visited.append(v+a)
      newdp[v+a] = max(olddp[v] + b, newdp[v+a])


if ans == 0:
  print(max(olddp))
else:
  print(ans)
### BUGGY CODE ###

import sys
input = sys.stdin.buffer.readline
import fractions

def main():
    N = int(input())
    a = list(map(int,input().split()))
    MOD = 10**9+7
            
    def lcm(x,y):
        return((x*y)//fractions.gcd(x,y))
        
    ans = 0
    l0 = 1
    for num in a:
        l1 = lcm(l0,num)
        ans = (ans*(l1//l0)+l1//num)%MOD
        l0 = l1
        
    print(ans)

if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
input = sys.stdin.buffer.readline
import fractions
from functools import reduce

def main():
    N = int(input())
    a = list(map(int,input().split()))
    MOD = 10**9+7
            
    def lcm(x,y):
        return((x*y)//fractions.gcd(x,y))
        
    l = reduce(lcm,a)
    ans = 0
    for num in a:
        ans += l//num
    
    print(ans%MOD)

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

s=input()

a=0
ab=0
abc=0
q=0

#if s[0]=="A" :
#  a=1
#elif s[0]=="?":
#  a=1
#  q=1
#print(str(a)+" "+str(ab)+" "+str(abc)+" "+str(q))

for i in range(len(s)):
  if s[i]=="A":
    a=a+3**q
  elif s[i]=="B":
    ab=ab+a
  elif s[i]=="C":
    abc=abc+ab
  elif s[i]=="?":
    abc=abc*3+ab
    
    ab=ab*3+a
    a=a*3
    
    a=a+3**q
    q+=1
  #print(str(a)+" "+str(ab)+" "+str(abc)+" "+str(q))
    
print(abc%(10**9+7))


### FIXED CODE ###

s=input()

a=0
ab=0
abc=0
q=1
m=10**9+7

for i in range(len(s)):
  if s[i]=="A":
    a=(a+q)%m
  elif s[i]=="B":
    ab=(ab+a)%m
  elif s[i]=="C":
    abc=(abc+ab)%m
  else:
    abc=(abc*3+ab)%m
    ab=(ab*3+a)%m
    a=(a*3+q)%m
    q=(q*3)%m
print(abc)
### BUGGY CODE ###

#C
def zeros(n): return [0 for i in range(n)]

N, M = [int(x) for x in input().split()]
city = []
for i in range(M):  
    pref, year = [int(x) for x in input().split()]
    city.append([pref, year, i, ''])

#print(N, M) #
#print(city) #
city.sort()
#print(city) #

nth = zeros(N+1)
for i in range(M):
    pref, year, seq, id = [x for x in city[i]]
    nth[pref] += 1
    city[i][3] = '%06d%06d' % (pref, nth[pref])
    #for j in range(4): print(city[i][j],end=',') #
for i in range(M):
    for j in range(M):
        if city[j][2]==i: print(city[j][3])



### FIXED CODE ###

#C
def zeros(n): return [0 for i in range(n)]

N, M = [int(x) for x in input().split()]
city = []
for i in range(M):  
    pref, year = [int(x) for x in input().split()]
    city.append([pref, year, i, ''])

#print(N, M) #
#print(city) #
city.sort()
#print(city) #

nth = zeros(N+1)
for i in range(M):
    pref, year, seq, id = [x for x in city[i]]
    nth[pref] += 1
    city[i][3] = '%06d%06d' % (pref, nth[pref])
    city[i][0] = city[i][2] # seqを最初に
    #for j in range(4): print(city[i][j],end=',') #
    
city.sort()
for i in range(M): print(city[i][3])
### BUGGY CODE ###

n = int(input())
l = [i for i in range(1, n + 1) if i % 2 == 1]
cnt = 0
for w in l:
    i = w
    fct = []
    b, e = 3, 0
while b * b < w:
    while i % b == 0:
        i = i // b
        e = e + 1
        if e > 0:
            fct.append((b, e))
            b, e = b + 1, 0
if i > 1:
    fct.append((i, 1))
if len(fct) > 1:
    r = 1
    for j in fct:
        r = r * (j[1] + 1)
elif len(fct) == 1:
    r = fct[0][1] + 1
else:
    r = 0
if r == 8:
    cnt += 1
print(cnt)

### FIXED CODE ###

n = int(input())
cnt = 0
for i in range(1, n + 1, 2):
    num_fact = 0
    for j in range(1, i + 1):
        if i % j == 0:
            num_fact += 1
    if num_fact == 8:
        cnt += 1
print(cnt)
### BUGGY CODE ###

N,Q=map(int,input().split())
S=input()
count_list=[]

for i in range(Q):
    l,r=map(int,input().split())
    l,r=l-1,r-1 #リストのインデックスとの整合
    print(S[l:r+1].count('AC'))

### FIXED CODE ###

N, Q = map(int, input().split())
S = input()
t = [0] * (N + 1)
for i in range(N):
    t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0)
for i in range(Q):
    l, r = map(int, input().split())
    print(t[r-1] - t[l-1])
### BUGGY CODE ###

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

for idx,i in enumerate(A):
    for j in A[idx+1:]:
        if i==j:
            print('NO')
            exit()
    
print('YES')

### FIXED CODE ###

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

if len(set(A))==len(A):
    print('YES')
else:
    print('NO')
### BUGGY CODE ###

H,W,K = map(int,input().split())
r1,c1,r2,c2 = map(lambda x:int(x)-1,input().split())
C = [input() for i in range(H)]

import heapq
dxy = [(0,1),(1,0),(0,-1),(-1,0)]
INF = float('inf')

dists = [[[INF]*4 for _ in range(W)] for _ in range(H)]
dists[r1][c1][0] = 0
hq = [(0,(r1*W+c1)*4)]
heapq.heapify(hq)
while hq:
    c,v = heapq.heappop(hq)
    p,d = divmod(v,4)
    y,x = divmod(p,W)
    if y==r2 and x==c2:
        print(0--c//K)
        exit()
    for di,(dx,dy) in enumerate(dxy):
        nx,ny = x+dx,y+dy
        if not 0 <= nx < W: continue
        if not 0 <= ny < H: continue
        if C[ny][nx] == '@': continue
        if di != d:
            nc = c + (K-c)%K
            if dists[y][x][di] <= nc: continue
            dists[y][x][di] = nc
            heapq.heappush(hq, (nc,p*4+di))
        else:
            if dists[ny][nx][di] <= c+1: continue
            heapq.heappush(hq, (c+1,(ny*W+nx)*4+di))
print(-1)

### FIXED CODE ###

import sys
input = sys.stdin.readline
H,W,K = map(int,input().split())
r1,c1,r2,c2 = map(lambda x:int(x)-1,input().split())
C = [input() for i in range(H)]

def solve():
    from collections import deque
    dxy = [(0,1),(1,0),(0,-1),(-1,0)]
    INF = float('inf')
    dists = [[INF]*W for _ in range(H)]
    dists[r1][c1] = 0
    q = deque([(r1,c1)])
    while q:
        y,x = q.popleft()
        if y==r2 and x==c2:
            print(dists[y][x])
            exit()
        for dx,dy in dxy:
            for i in range(1,K+1):
                nx,ny = x+dx*i,y+dy*i
                if not 0 <= nx < W: break
                if not 0 <= ny < H: break
                if C[ny][nx] == '@': break
                if dists[ny][nx] <= dists[y][x]: break
                if dists[ny][nx] <= dists[y][x] + 1: continue
                dists[ny][nx] = dists[y][x] + 1
                q.append((ny,nx))
    print(-1)
solve()
### BUGGY CODE ###

from itertools import accumulate

def main():
    N = int(input())
    A = tuple(map(int, input().split()))

    a = [0] + list(accumulate(A))
    cnt = 0
    fin = set()
    for i in a:
        n = a.count(i) - 1
        if i not in fin:
            cnt += n * (n + 1) // 2
            fin.add(i)
    print(cnt)

main()


### FIXED CODE ###

from itertools import accumulate
from collections import Counter

def main():
    N = int(input())
    A = tuple(map(int, input().split()))

    a = [0] + list(accumulate(A))
    num = Counter(a)
    lst = set(a)
    cnt = 0
    for i in lst:
        n = num[i] - 1
        cnt += n * (n + 1) // 2
    print(cnt)

main()
### BUGGY CODE ###

N, Q = map(int, input().split())
value = [0]*(N+1)
tree = [[i] for i in range(N+1)]
for i in range(N-1):
  a, b = map(int, input().split())
  for rel in tree:
    if a in rel:
      rel.append(b)
for i in range(Q):
  a, b = map(int, input().split())
  for arg in tree[a]:
    value[arg] += b
print(' '.join(map(str, value[1:])))

### FIXED CODE ###

N, Q = map(int, input().split())
value = [0]*(N+1)
ab_list = []
for i in range(N-1):
  a, b = map(int, input().split())
  ab_list.append([a, b])
ab_list.sort(key=lambda x:x[0])
for i in range(Q):
  p, q = map(int, input().split())
  value[p] += q
for a, b in ab_list:
  value[b] += value[a]
print(' '.join(map(str, value[1:])))
### BUGGY CODE ###

from functools import reduce


# fin = open('mujin_a.txt')
# input = fin.readline
N = int(input())
p_list = [int(x) for x in input().strip().split()]

pat = 1
sum = 0
for i, r_pos in enumerate(p_list):
    if r_pos >= 2 * sum + 1:
        sum += 1
    else:
        pat *= sum + 1

print(reduce(lambda x, y: x * y, range(1, sum + 1), pat) % 1000000007)

### FIXED CODE ###

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

pat = 1
sum = 0
for i, r_pos in enumerate(p_list):
    pat = (pat * (sum + 1)) % 1000000007
    if r_pos >= 2 * sum + 1: sum += 1
print(pat)
### BUGGY CODE ###

import numpy as np

H, W = map(int, input().split())

maze_x = [input() for _ in range(H)]
maze_y = [''.join(x) for x in zip(*maze_x)]

def count_light(maze, W, H):
    cnt = [[0]*W for _ in range(H)]
    for hh in range(H):
        lst1 = [i for i in maze[hh].split('#') if i != '']
        ww = 0
        k = 0
        while 0 <= ww <= W - 1:
            if maze[hh][ww] == '.':
                ln = len(lst1[k])
                cnt[hh][ww:ww+ln] = [ln]*ln
                ww += ln-1
                if len(lst1)==k:break
                k += 1
            ww += 1
    return cnt
cnt = (np.array(count_light(maze_x, W, H)) + np.array(count_light(maze_y, H, W)).T -1)

print(max([max(cnt[i]) for i in range(H)]))


### FIXED CODE ###

import numpy as np

H, W = map(int, input().split())

maze01 = np.array([[int(x=='.') for x in input()] for _ in range(H)]) 

u = np.zeros((H, W), dtype=np.int32)
d = np.zeros((H, W), dtype=np.int32)
r = np.zeros((H, W), dtype=np.int32)
l= np.zeros((H, W), dtype=np.int32)


u[0] = maze01[0]
d[H - 1] = maze01[H - 1]
for hh in range(1,H):
    u[hh] = u[hh-1] + maze01[hh]
    u[hh] *= maze01[hh]
    d[H - hh - 1] = d[H - hh] + maze01[H - hh - 1]
    d[H - hh - 1] *= maze01[H - hh - 1]

l[:, 0] = maze01[:, 0]
r[:, W - 1] = maze01[:, W - 1]
for ww in range(1, W):
    l[:, ww] = l[:, ww -1] + maze01[:, ww]
    l[:, ww] *= maze01[:, ww]
    r[:, W - ww -1] = r[:, W - ww] + maze01[:, W - ww - 1]
    r[:, W - ww -1] *= maze01[:, W - ww - 1]

cnt = r + l + u + d - 3

print(max([max(cnt[i]) for i in range(H)]))
### BUGGY CODE ###

N = int(input())
s = [input().split() for i in range(N)]
for i in range(N):
    s[i] = list(s[i][0])
    s[i].sort()
s.sort()
s_af = [""]*N
for i in range(N):
    for k in range(10):
        s_af[i] += s[i][k]
count = 0
for i in range(N):
    for j in range(i+1,N):
        if s_af[i] == s_af[j]:
            count += 1
        else:
            break
print(count)

### FIXED CODE ###

N = int(input())
s = [input().split() for i in range(N)]
for i in range(N):
    s[i] = list(s[i][0])
    s[i].sort()
    s[i] = ''.join(map(str, s[i]))
s.sort()
count = 0
ren = 0
for i in range(N-1):
    if s[i] == s[i+1]:
        count += 1+ren
        ren += 1
    else:
        ren = 0
print(count)
### BUGGY CODE ###

from collections import Counter

a = input()
n = len(a)

res = [-1] * (n+1) 
res[n] = 0
for i in range(n-1, -1, -1):
    res[i] = (res[i+1] + 10**(n-i-1) * int(a[i]) % 2019) % 2019

c = Counter(res)
print(int(sum([i*(i-1)/2 for i in c.values()])))

### FIXED CODE ###

a = input()
n = len(a)

s = 0 # s[0]の余り
c = [0] * 2019 # c[i] = 余り i の個数
c[s] += 1
d = 1 # 10**i mod 2019

for i in range(n):
    s = (s + d * int(a[n-i-1])) % 2019 # s[i+1]の余り
    d = d * 10 % 2019 #10**(i+1) mod 2019
    c[s] += 1

print(int(sum([i*(i-1)/2 for i in c])))
### BUGGY CODE ###

H,W = map(int,input().split())
Ch,Cw = map(int,input().split())
Dh,Dw = map(int,input().split())
S = [list(input()) for i in range(H)]
 
res_S = [[-1]*W for i in range(H)]
dx = [1,-1,0,0]
ddx = [-2,-1,0,1,2,-2,-1,1,2,-2,2,-2,-1,1,2,-2,-1,0,1,2]
dy = [0,0,1,-1]
ddy = [-2,-2,-2,-2,-2,-1,-1,-1,-1,0,0,1,1,1,1,2,2,2,2,2]
count = 0
 
que = [(Ch,Cw)]
que_warp =[(Ch,Cw)]
res_S[Ch-1][Cw-1] = 0
 
while (que or que_warp):
    while (que):
        que_warp.append(que[0])
        for i in range(4):
            nx = int(que[0][0]+dx[i])
            ny = int(que[0][1]+dy[i])
            if (1<=nx<=H) and (1<=ny<=W):
                if res_S[nx-1][ny-1]<0 and S[nx-1][ny-1]=='.':
                    res_S[nx-1][ny-1]=count
                    que.append((nx,ny))
        que.pop(0)
 
    count += 1
 
    while(que_warp):
        for i in range(len(ddx)):
            nx = int(que_warp[0][0] + ddx[i])
            ny = int(que_warp[0][1] + ddy[i])
            if (1 <= nx <= H) and (1 <= ny <= W):
                if res_S[nx-1][ny-1]<0 and S[nx-1][ny-1]=='.':
                    res_S[nx-1][ny-1] = count
                    que.append((nx, ny))
        que_warp.pop(0)
 
print (res_S[Dh-1][Dw-1])


### FIXED CODE ###

H,W = map(int,input().split())
Ch,Cw = map(int,input().split())
Dh,Dw = map(int,input().split())
S = [list(input()) for i in range(H)]
 
res_S = [[-1]*W for i in range(H)]
dx = [1,-1,0,0]
ddx = [-2,-1,0,1,2,-2,-1,1,2,-2,2,-2,-1,1,2,-2,-1,0,1,2]
dy = [0,0,1,-1]
ddy = [-2,-2,-2,-2,-2,-1,-1,-1,-1,0,0,1,1,1,1,2,2,2,2,2]
count = 0
 
que = [(Ch,Cw)]
que_warp =[(Ch,Cw)]
res_S[Ch-1][Cw-1] = 0
 
while (que or que_warp):
    l = len(que)
    while (que):
        if l > 0:
            que_warp.append(que[0])
            l -= 1
        for i in range(4):
            nx = int(que[0][0]+dx[i])
            ny = int(que[0][1]+dy[i])
            if (1<=nx<=H) and (1<=ny<=W):
                if res_S[nx-1][ny-1]<0 and S[nx-1][ny-1]=='.':
                    res_S[nx-1][ny-1]=count
                    que.append((nx,ny))
                    que_warp.append((nx,ny))
        que.pop(0)
 
    count += 1
 
    while(que_warp):
        for i in range(len(ddx)):
            nx = int(que_warp[0][0] + ddx[i])
            ny = int(que_warp[0][1] + ddy[i])
            if (1 <= nx <= H) and (1 <= ny <= W):
                if res_S[nx-1][ny-1]<0 and S[nx-1][ny-1]=='.':
                    res_S[nx-1][ny-1] = count
                    que.append((nx, ny))
        que_warp.pop(0)
 
print (res_S[Dh-1][Dw-1])
### BUGGY CODE ###

import sys
N=int(input())
if N%2==0:
  print(N//2,N,N)
else:
  for i in range(1,10**5):
    for j in range(i+1):
      if 3*i*j==N*(i+j):
        print(N,i,j)
        sys.exit()

### FIXED CODE ###

import sys
N=int(input())
if N%2==0:
  print(N//2,N,N)
  sys.exit()
for a in range(1,3501):
  for b in range(1,a):
    M=N*a*b
    L=4*a*b-N*(a+b)
    if L>0:
      if M%L==0:
        print(a,b,M//L)
        sys.exit()
### BUGGY CODE ###

from collections import Counter

def ans_solve(N, As):
    C = Counter(As)
    ans = 0
    while len(C):
        d = max(C.keys())
        C[d] -= 1
        if C[d] == 0:
            del C[d]

        n = (2 ** d.bit_length()) - d
        if n in C:
            C[n] -= 1
            ans += 1
            if C[n] == 0:
                del C[n]
    return ans


if __name__ == "__main__":
    N = input()
    As = [int(c) for c in input().split()]
    print(ans_solve(N, As))


### FIXED CODE ###

from collections import Counter

def ans_solve(N, As):
    C = Counter(As)
    ans = 0
    for d in reversed(sorted(As)):
        n = (2 ** d.bit_length()) - d
        if d != n:
            if C[d] > 0 and C.get(n, 0) > 0:
                C[d] -= 1
                C[n] -= 1
                ans += 1
        else:
            ans += C[d]//2
            C[d] = 0
    return ans
 
 
if __name__ == "__main__":
    N = input()
    As = [int(c) for c in input().split()]
    print(ans_solve(N, As))
### BUGGY CODE ###

n=int(input())
li = list(map(int,input().split()))
if li.count(1)==0:
    print(-1)
else:
    del li[0:li.index(1)]
    for k in range (2,n+1):
        if li.count(k)==0:
            del li[k-1:]
            break
        else:
            del li[k-1:li.index(k)]
    print(n-len(li))


### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
x=1
ans=0
for i in range(n):
    if a[i]!=x:
        ans+=1
    else:
        x+=1
if ans==n:
    print(-1)
else:
    print(ans)
### BUGGY CODE ###

def flpl(): return [float(i) for i in input().split()]
def inpl(): return [int(i) for i in input().split()]
import numpy as np
import heapq

xs, ys, xt, yt = flpl()
rs,rt = 0, 0
N = int(input())
xyr = [(xs, ys, rs),(xt, yt,rt)] + [inpl() for _ in range(N)]
X, Y, R = np.array(xyr).T
X, Y, R = np.tile(X, (N+2, 1)), np.tile(Y, (N+2, 1)), np.tile(R, (N+2, 1))
edge = np.maximum(np.zeros((N+2, N+2)), np.sqrt(np.square(X-X.T) +\
                  np.square(Y-Y.T))-(R+R.T)) +\
                  np.diag(np.ones(N+2)*np.inf)

dist = np.array([np.inf for _ in range(N+2)])
dist[0] = 0 
remain = set(range(N+2))
Q = []
heapq.heappush(Q,(0,0))
while Q:
    k = heapq.heappop(Q)
    k = k[1]
    if k not in remain:
        continue
    remain.remove(k)
    for j in remain:
        if dist[k] + edge[k][j] < dist[j]:
            dist[j] = dist[k] + edge[k][j] 
            heapq.heappush(Q,(dist[j],j)) 
    if 1 not in remain:
        break
print(dist[1])

### FIXED CODE ###

def flpl(): return [float(i) for i in input().split()]
def inpl(): return [int(i) for i in input().split()]
import heapq
def dijkstra(vnum, vs):
    dist = [float('inf') for _ in range(vnum)]
    dist[vs] = 0 
    remain = set(range(vnum))
    Q = []
    heapq.heappush(Q,(0,vs))
    while Q:
        c, vn = heapq.heappop(Q)
        if vn not in remain :
            continue
        remain.remove(vn)
        for j in remain:
            evnj = edge(vn, j)
            if dist[vn] + evnj < dist[j]:
                dist[j] = dist[vn] + evnj 
                heapq.heappush(Q,(dist[j],j)) 
        if not remain:
            break
    return dist
def edge(s, t):
    return max(((X[s] - X[t])**2 + (Y[s] - Y[t])**2)**(1/2) -(R[s]+R[t]), 0)

xs, ys, xt, yt = flpl()
N = int(input())
xyr = [(xs, ys, 0),(xt, yt, 0)] + [inpl() for _ in range(N)]
X, Y, R = list(map(list, zip(*xyr)))
D = dijkstra(N+2, 0)
print(D[1])
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
ans = 0
p = [i + A[i] for i in range(N)]
m = [j - A[j] for j in range(N)]
a = min(p)
b = max(m)
for i in range(a,b+1):
    if i in p and i in m:
        ans += p.count(i) * m.count(i)
print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
ans = 0
p = {}
m = {}
for i in range(N):
    if p.get(i+A[i]) is None:
        p[i+A[i]] = 1
    else:
        p[i+A[i]] += 1
for i in range(N):
    if m.get(i-A[i]) is None:
        m[i-A[i]] = 1
    else:
        m[i-A[i]] += 1
for x in range(min(p),max(m)+1):
    if x in p and x in m:
        ans += p[x] * m[x]
print(ans)
### BUGGY CODE ###

def main():
    L, R = list(map(int, input().split()))
    minmod = 10000000
    for i in range(L, R):
        for j in range(i+1, R+1):
            prod = i * j
            mod = prod % 2019
            if mod < minmod:
                minmod = mod
    print(minmod)

main()

### FIXED CODE ###

def main():
    L, R = list(map(int, input().split()))
    minmod = 10000000
    for i in range(L, R):
        for j in range(i+1, R+1):
            prod = i * j
            mod = prod % 2019
            #mod = np.mod([prod], 2019)[0]
            if mod < minmod:
                minmod = mod
            if minmod < 1:
                break
        else: continue
        break
    print(minmod)

main()