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

import numpy as np
import math
import collections
import bisect


def divisor(n):
    i = 1
    table = []
    while(i * i <= n):
        if n % i == 0:
            table.append(i)
            table.append(n // i)
        i += 1
    table = list(set(table))
    return table


N = int(input())

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

A.sort()

ans = 0
ans = N
for i in range(N):
    divs = divisor(A[i])
    Set = set(A[:i])
    #print(A[i], "divs", divs)
    if i < N - 1:
        if A[i] == A[i + 1]:
            ans -= 1
            continue

    for d in divs:
        if i != 0:
            if d in Set:
                ans -= 1
                break
print(ans)


### FIXED CODE ###

import numpy as np
import math
import collections
import bisect
from collections import Counter


N = int(input())

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

A.sort()
Set = set()

Amax = A[len(A) - 1]

counter = Counter(A)
ans = 0
for a in A:
    if a in Set:
        continue
    x = a
    while(x <= Amax):
        Set.add(x)
        x += a

    if counter[a] == 1:
        ans += 1

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

import sys
sys.setrecursionlimit(10**7)
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 LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

from collections import deque

def main():
    N = II()
    a_mat = []
    for i in range(N):
        a_mat.append(deque(LI()))

    cnt = 0
    first_list = [a_list.popleft() for a_list in a_mat]
    while True:
        # この日に試合できる可能性リスト
        # 試合する人リスト
        match_list = []
        for i, a in enumerate(first_list):
            ind = i+1
            # 試合をするペアの要素を入れていく
            if first_list[a-1] == ind:
                match_list.append(i)

        if len(match_list) == 0:
            print(-1)
            return

        # 試合をしたことを反映
        for i in match_list:
            if len(a_mat[i]) == 0:
                first_list[i] = 0
            else:
                first_list[i] = a_mat[i].popleft()

        # 一日経過
        cnt += 1

        # 終了判定
        if sum(first_list) == 0:
            break

    print(cnt)


main()

### FIXED CODE ###

import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())


from collections import deque

from copy import copy
#debug = True
debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)
def main():
    N = II()
    a_mat = []
    for i in range(N):
        a_mat.append(deque(LI()))

    cnt = 0
    first_list = [a_list.popleft() for a_list in a_mat]
    nend = 0
    previous_match_list = list(range(N))
    while True:
        # この日に試合できる可能性リスト
        # 試合する人リスト
        match_list = []
        for i in previous_match_list:
            a_no = first_list[i]
            a = a_no-1
            no = i+1
            # 試合をするペアの要素を入れていく
            if first_list[a] == no:
                match_list.append(i)

        dprint(first_list, match_list)

        if len(match_list) == 0:
            print(-1)
            return

        previous_match_list = copy(match_list)

        # 試合をしたことを反映
        for i in match_list:
            if len(a_mat[i]) == 0:
                first_list[i] = 0
                nend += 1 # 終わった人
            else:
                next_no = a_mat[i].popleft()
                next = next_no - 1
                first_list[i] = next_no
                previous_match_list.append(next)

        previous_match_list = list(set(previous_match_list))

        dprint(match_list, previous_match_list)
        dprint("")
        # 一日経過
        cnt += 1

        # 終了判定
        if nend == N:
            break


    print(cnt)


main()
### BUGGY CODE ###

S = input()
times = int(input())

for i in range(times):
    Q =input().split()
    if Q[0] =="1":
        S = S[::-1]
    else:
        if Q[1] == "1":
            S = Q[2] +S
        else:
            S = S + Q[2]

print(S)

### FIXED CODE ###

S = input()
times = int(input())

top =0
first = str()
last = str()

for i in range(times):
    Q =input().split()
    if Q[0] =="1":
        top = (top+1)%2
    else:
        if Q[1] == "1":
            #先頭に追加する 0のとき先頭
            if top ==0:
                first +=Q[2]
            else:
                last += Q[2]
        else:
            if top ==1:
                first +=Q[2]
            else:
                last += Q[2]
if top == 0:
    S = first[::-1] + S + last
else:
    S = last[::-1] + S[::-1] + first


print(S)
### BUGGY CODE ###

N, M = map(int, input().split())
line = []

for i in range(M):
    ab = list(map(int, input().split()))
    line.append(ab)

start = 1
end = N
second = []

for l in line:
    if l[0] == start:
        second.append(l[1])
    elif l[1] == start:
        second.append(l[0])

for l in line:
    if l[0] in second:
        if l[1] == end:
            print('POSSIBLE')
            exit()
    if l[1] in second:
        if l[0] == end:
            print('POSSIBLE')
            exit()
print('IMPOSSIBLE')


### FIXED CODE ###

N, M = map(int, input().split())
line = []

for i in range(M):
    ab = list(map(int, input().split()))
    line.append(ab)

start = 1
end = N
first = set()
second = set()

for l in line:
    if end in l:
        second.add(l[0])
        second.add(l[1])
    elif start in l:
        first.add(l[1])
        first.add(l[0])


for f in first:
    if f in second:
        print('POSSIBLE')
        exit()

print('IMPOSSIBLE')
### BUGGY CODE ###

ht, st, ha, sa = list(map(int, input().split(' ')))
bool_player = True  # Takashi
while (ht > 0 or st > 0):
    if bool_player:
        ha -= st
        bool_player = False
    else:
        ht -= sa
        bool_player = True

if ht <= 0:
    print('No')
else:
    print('Yes')


### FIXED CODE ###

ht, st, ha, sa = list(map(int, input().split(' ')))
bool_player = True  # Takashi
while (ht > 0 and ha > 0):
    if bool_player:
        ha -= st
        bool_player = False
    else:
        ht -= sa
        bool_player = True

if ht <= 0:
    print('No')
else:
    print('Yes')
### BUGGY CODE ###

a,b=map(int,input().split())
aa=[a,b]
for i in range(a,b):
  if not i in aa:
    aa.append(i)
z=aa.count(0)
n=sum([1 for i in aa if i<0])%2
if z>0:
  print('Zero')
elif n==0:
  print("Positive")
else:
  print("Negative")

### FIXED CODE ###

a,b=map(int,input().split())
a=min(1,a)
b=min(1,b)
if b==1 and a<=0:
  print('Zero')
elif b==0:
  print('Zero')
elif (b-a)%2==0 and b<0:
  print("Negative")
else:
  print("Positive")
### BUGGY CODE ###

N = int(input())
S = input()
ans1 = 0
for i in range(1000):
  ans = False
  for j in range(N):
    if S[j] == str(i).zfill(3)[0]:
      for k in range(j+1, N):
        if S[k] == str(i).zfill(3)[1]:
          for l in range(k+1, N):
            if S[l] == str(i).zfill(3)[2]:
              ans = True
              break
        if ans:
          break
    if ans:
      break
  if ans:
    ans1 += 1
print(ans1)

### FIXED CODE ###

input()
S = [int(i) for i in input()]
cnt = 0
for i in range(0, 1000):
  c = (i//100, (i//10)%10, i%10)
  f = 0
  for s in S:
    if s == c[f]:
      f += 1 
      if f==3:break 
  if f==3:cnt += 1
print(cnt)
### BUGGY CODE ###

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

# dp[i][j] = a[i:j] を考えたときの X-Y の最大値
dp = [[0]*(N+1) for _ in range(N+1)]
for l in range(1, N+1):
    for i in range(N-l+1):
        j = i+l
        if (N-l)%2 == 0:
            dp[i][j] = max(dp[i+1][j]+a[i], dp[i][j-1]+a[j-1])
        else:
            dp[i][j] = min(dp[i+1][j]-a[i], dp[i][j-1]-a[j-1])
        
print(dp[0][N])

### FIXED CODE ###

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

# dp[i][j] = a[i:j] を考えたときの X-Y の最大値
dp = [[0]*(N+1) for _ in range(N+1)]
for l in range(1, N+1):
    for i in range(N-l+1):
        j = i+l
        if (N-l)%2 == 0:
            dp[i][j] = max(dp[i+1][j]+a[i], dp[i][j-1]+a[j-1])
        else:
            dp[i][j] = min(dp[i+1][j]-a[i], dp[i][j-1]-a[j-1])
        
print(dp[0][N])
### BUGGY CODE ###

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

S = []
for i in range(H):
  S.append(list(input()))

dp = [[0] * W for h in range(H)]
dp1 = [[[0, 0] for w in range(W+1)] for h in range(H+1)]
for h in range(H):
  for w in range(W):
    if S[h][w] == '#':
      continue
    
    dp1[h+1][w+1] = [dp1[h][w+1][0]+1, dp1[h+1][w][1]+1]
    dp[h][w] = sum(dp1[h+1][w+1])


ans = 0
dp2 = [[[0, 0] for w in range(W+1)] for i in range(H+1)]
for h in range(H-1, -1, -1):
  for w in range(W-1, -1, -1):
    if S[h][w] == '#':
      continue

    dp2[h][w] = [dp2[h+1][w][0]+1, dp2[h][w+1][1]+1]
    dp[h][w] += sum(dp2[h][w])
    ans = max(ans, dp[h][w]-3)

print(ans)


### FIXED CODE ###

import numpy as np

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

S = np.ones([H+2, W+2], dtype=bool)
for h in range(H):
  S[h+1] = np.array(['.'] + list(input()) + ['.']) == '.'

dp1 = np.zeros([H+2, W+2, 2], dtype=int) # [up, left]
for h in range(H):
  dp1[h+1, :, 0] = (dp1[h, :, 0] + 1)*S[h+1]

for w in range(W):
  dp1[:, w+1, 1] = (dp1[:, w, 1] + 1)*S[:, w+1]

dp2 = np.zeros([H+2, W+2, 2], dtype=int) # [down, right]
for h in range(H, 0, -1):
  dp2[h, :, 0] = (dp2[h+1, :, 0] + 1)*S[h]

for w in range(W, 0, -1):
  dp2[:, w, 1] = (dp2[:, w+1, 1] + 1)*S[:, w]

print(np.max((np.sum(dp1, axis=2)+np.sum(dp2, axis=2))[1:-1, 1:-1])-3)
### BUGGY CODE ###

K,S = map(int, input().split())
count = 0
for x in range(K+1):
    for y in range(K+1):
        for z in range(K+1):
            if x + y + z == S:
                count += 1
print(count)

### FIXED CODE ###

K,S = map(int, input().split())
count = 0
for x in range(K+1):
    for y in range(K+1):
        z = S-x-y
        if z >=0 and z <=K:
            count += 1
print(count)
### BUGGY CODE ###

import numpy as np
N = int(input())
P = np.array(list(map(int, input().split())))
ans = 0

for i in range(N):
  l = np.array([P[i]] * N)
  tmp = list(P - l)[0:i+1]
  if min(tmp) >= 0:
    ans += 1

print(ans)

### FIXED CODE ###

N = int(input())
P = list(map(int, input().split()))
ans = 0
min_number = 2*10**5+1

for i in range(N):
  min_number = min(min_number, P[i])
  if min_number == P[i]:
    ans += 1
  
print(ans)
### BUGGY CODE ###

import sys

def dp_all(dp):
  length = len(dp)
  for i in range(length):
    print(dp[i])

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

a = []
b = []

for i in range(N):
  a_dash,b_dash = map(int,input().split())
  a.append(a_dash)
  b.append(b_dash)

#dp[i+1][j] = i番目までの魔法で体力jのモンスターに
#勝つことができる、魔力の最小値
INF = 10**9
dp = [[INF for _ in range(H+1)] for _ in range(N+1)]
#dp[0] = [0 for _ in range(H+1)]

for i in range(N):
  for j in range(H+1):
    if j == 0:
      dp[i+1][0] = 0
    elif a[i] > j:
      dp[i+1][j] = min(dp[i][j],b[i])
    else:
      dp[i+1][j] = min(dp[i][j],dp[i+1][j-a[i]]+ b[i])

print(dp[N][H])


### FIXED CODE ###

import sys

def dp_all(dp):
  length = len(dp)
  for i in range(length):
    print(dp[i])

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

a = []
b = []

for i in range(N):
  a_dash,b_dash = map(int,input().split())
  a.append(a_dash)
  b.append(b_dash)

#dp[i+1][j] = i番目までの魔法で体力jのモンスターに
#勝つことができる、魔力の最小値
INF = 10**9
dp = [[INF for _ in range(H+1)] for _ in range(N+1)]

for i in range(N+1):
  dp[i][0] = 0

for i in range(N):
  for j in range(H+1):
    if a[i] > j:
      dp[i+1][j] = min(dp[i][j],b[i])
    else:
      dp[i+1][j] = min(dp[i][j],dp[i+1][j-a[i]]+ b[i])

print(dp[N][H])
### BUGGY CODE ###

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

ans=0
for a in set(A):
  if A.count(a)==a:
    continue
  elif A.count(a)<a:
    ans+=A.count(a)
  else:
    ans+=min(A.count(a)-a,A.count(a))
print(ans)

### FIXED CODE ###

from collections import Counter
n=int(input())
A=list(map(int,input().split()))

c=Counter(A)
ans=0
for key, value in c.items():
  if key!=value:
    if key>value:
      ans+=value
    else:
      ans+=value-key
print(ans)
### BUGGY CODE ###

S=input()
Q=int(input())
import sys


for _ in range(Q):
  s = list(sys.stdin.readline())
  if s[0]=="1":
    S=S[::-1]
  else:
    if s[2]=="1":
      S="".join([s[4],S])
    else:
      S="".join([S,s[4]])
      
print(S)

### FIXED CODE ###

S=input()
Q=int(input())
import sys
from collections import deque
d=deque()
for _ in S:
  d.append(_)
flag=False

for _ in range(Q):
  s = list(sys.stdin.readline())
  if s[0]=="1":
    if flag:
      flag=False
    else:
      flag=True
  else:
    if s[2]=="1":
      if flag:
        d.append(s[4])
      else:
        d.appendleft(s[4])
    else:
      if flag:
        d.appendleft(s[4])
      else:
        d.append(s[4])
      
if flag:
  d.reverse()
  
print("".join(d))
### BUGGY CODE ###

# ABC155 C

n = int(input())
S = []

for _ in range(n):
    S.append(input())

set_S = list(set(S))
count_S = []

for string in set_S:
    count_S.append(S.count(string))

max_S = max(count_S)

index_nums = [n for n, v in enumerate(count_S) if v == max_S]
res = []

for i in index_nums:
    res.append(set_S[i])

for _ in sorted(res):
    print(_)

### FIXED CODE ###

# ABC155 C

n = int(input())
S = {}
max_S = 0
res = []

for _ in range(n):
    s = input()
    if s in S:
        S[s] += 1
        if S[s] > max_S:
            max_S = S[s]
    else:
        S[s] = 1
        if S[s] > max_S:
            max_S = S[s]

for key in S:
    if S[key] == max_S:
        res.append(key)

for _ in sorted(res):
    print(_)
### BUGGY CODE ###

def search(S):
    cnt = 0
    N = len(S)
    for i in range(N+1):
        for j in range(i+1, N+1):
            num = S[i:j]
            if int(num)%2019==0:
                cnt += 1
                i += 3
    return cnt 

if __name__ == "__main__":
    S = input()
    cnt = search(S)
    print(cnt)

### FIXED CODE ###

S = list(map(int, list(input())))[::-1]
N = len(S)
CNT = [0]*2019
CNT[0] = 1
num = 0
d = 0
for i in range(N):
    num += S[i]*pow(10, d, 2019)
    num %= 2019
    CNT[num] += 1
    d += 1
ans = 0 
for cnt in CNT:
    ans += (cnt*(cnt - 1)) // 2
print(ans)
### BUGGY CODE ###

n = int(input())
list_in = list(map(int, input().split()))
q = int(input())
for i in range(q):
    bc = list(map(int, input().split()))
    for j in range(n):
        if list_in[j] == bc[0]:
            list_in[j] = bc[1]
    print(sum(list_in))

### FIXED CODE ###

import collections
n = int(input())
list_in = list(map(int, input().split()))
q = int(input())

counter = collections.Counter(list_in)
sum = sum(list_in)

for i in range(q):
    bc = list(map(int, input().split()))
#    list_in = [bc[1] if list_in[l] == bc[0] else list_in[l] for l in range(n)]
    sum_diff = (bc[1] - bc[0]) * counter[bc[0]]
    counter[bc[1]] = counter[bc[1]] + counter[bc[0]]
    counter[bc[0]] = 0
    print(sum+sum_diff)
    sum = sum + sum_diff
### BUGGY CODE ###

import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
len_aaa = len(aaa)
aaa = sorted([-a for a in aaa])

for i in range(m):
    b, c = list(map(int, input().split(' ')))
    ccc = [-c] * min(b, len_aaa)
    index = bisect.bisect_left(aaa, -c)
    aaa = (aaa[:index] + ccc + aaa[index:])[:len_aaa]
print(abs(sum(aaa)))

### FIXED CODE ###

import bisect
n, m = list(map(int, input().split(' ')))
aaa = list(map(int, input().split(' ')))
aaa = sorted([-a for a in aaa])[::-1]
len_aaa = len(aaa)

cccbbb = []
for i in range(m):
    b, c = list(map(int, input().split(' ')))
    cccbbb.append((-c, b))
cccbbb = sorted(cccbbb)
ccc = []
for c, b in cccbbb:
    ccc += [c] * b
    if len(ccc) > len_aaa:
        break
ans = 0
for i in range(len_aaa):
    c = ccc[i] if i < len(ccc) else 0
    ans += min(aaa[i], c)
print(abs(ans))
### BUGGY CODE ###

n=int(input())
h=[int(i) for i in input().split()]
def frog(x):
    if len(x)<=3:
        return(abs(x[0]-x[-1]))

    else:
        step1=abs(x[0]-x[1])
        step2=abs(x[0]-x[2])
        return min(step1+frog(x[1:]),step2+frog(x[2:]))

print(frog(h))


### FIXED CODE ###

n=int(input())
h=[int(i) for i in input().split()]
dp=[0]*n
dp[1]=abs(h[0]-h[1])
for i in range(2,n):
    dp[i]=min(dp[i-2]+abs(h[i-2]-h[i]),dp[i-1]+abs(h[i-1]-h[i]))
print(dp[n-1])
### BUGGY CODE ###

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

for i in range(n):
    print(a.count(i+1))

### FIXED CODE ###

n = int(input()) 
a = list(map(int, input().split()))
x = [0]*n
for i in a:
    x[i-1] += 1
for _ in x:
    print(_)
### BUGGY CODE ###

# coding: utf-8
# Your code here!
S=input()
 
sum=0
judge=True

i=0
while i<len(S)-1:
    if S[i:i+2]=="BW":
        S=S[:i]+"WB"+S[i+2:]
        sum+=1
        i-=2
    i=max(0,i+1)
    
print(sum)



### FIXED CODE ###

# coding: utf-8
# Your code here!
S=input()
N=len(S)

sum=0
k=0

for i in range(N):
    if S[i]=="W":
        sum+=(i-k)
        k+=1

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

n,m = map(int,input().split())
d = {}
for i in range(n):
  a,b = map(int,input().split())
  if a in d.keys():
    d[a] += b
  else:
    d.setdefault(a,b)
l = list(d.keys())
l.sort()
count = 0
mon = 0
i = 0
while count < m:
  mon += l[i]*d[l[i]]
  count += d[l[i]]
  i += 1
i -= 1
if count > m:
  mon -= (count-m)*l[i]
print(mon)

### FIXED CODE ###

import sys
input = sys.stdin.readline
n,m = map(int,input().split())
l = []
for i in range(n):
  a,b = map(int,input().split())
  l.append([a,b])
l.sort()
i = 0
count = 0
money = 0
while count < m:
  count += l[i][1]
  money += l[i][0]*l[i][1]
  i += 1
i -= 1
if count > m:
  money -= (count-m)*l[i][0]
print(money)
### BUGGY CODE ###

N = int(input())
A = []
for i in range(N):
  A.append(input())

max_itr = [i for i, v in enumerate(A) if v == max(A)]
max_number = max(A)
if len(max_itr) == 1:
  for i in range(N):
    if i != max_itr[0]:
      print(max_number)
    else:
      print(sorted(A)[-2])
else:
  for i in range(N):
    print(max_number)

### FIXED CODE ###

import numpy as np
N = int(input())
A = []
for i in range(N):
  A.append(int(input()))

npA = np.array(A)
max_number = max(A)
max_itr = np.where(npA == npA.max())
if len(max_itr[0]) == 1:
  for i in range(N):
    if i != max_itr[0][0]:
      print(max_number)
    else:
      print(sorted(A)[-2])
else:
  for i in range(N):
    print(max_number)
### BUGGY CODE ###

from typing import List
import math


def solution(K: int) -> int:
    ans: int = 0
    x: int = 7
    while True:
        # print(x)
        # print()
        ans += 1
        # K が偶数の時
        if K % 2 == 0:
            return -1
        # x が K より小さい時
        if x < int(K):
            x: int = int(str(x) + str(7))
            continue
        if x % K == 0:
            return ans
        if K < ans:
            return -1
        x: int = int(str(x) + str(7))
    return ans


K: int = int(input())

print(solution(K))


### FIXED CODE ###

def solution(K: int) -> int:
    sevens: int = 0
    for i in range(1, K + 1):
        sevens = (sevens*10 + 7) % K
        if sevens == 0:
            return i
    return -1


K: int = int(input())

print(solution(K))
### BUGGY CODE ###

#!/usr/bin/python

# AtCoder Beginner Contest 084
# D: 2017-like Number

import sys, math

MAX_N = 100000
isprime_ = []

def sieve():
  global isprime_, MAX_N
  isprime_ = [i >= 2 for i in range(0, MAX_N + 1)]
  for p in range(2, MAX_N + 1):
    if isprime_[p]:
      for q in range(p + p, MAX_N + 1, p):
        isprime_[q] = False

def isPrime(n):
  global isprime_
  return isprime_[n]

def is2017like(n):
  return isPrime(n) and isPrime((n + 1) // 2)

# the number of 2017-like numbers between l and r.
def solve(l, r):
  return len([i for i in range(l, r + 1, 2) if is2017like(i)])

sieve()
Q = int(sys.stdin.readline())
for i in range(Q):
  l, r = [int(w) for w in sys.stdin.readline().split()]
  print(solve(l, r))


### FIXED CODE ###

#!/opt/local/bin/python3

# AtCoder Beginner Contest 084
# D: 2017-like Number

import sys, math
from itertools import accumulate

MAX_N = 100000

def sieve():
  isprime = [i >= 2 for i in range(MAX_N + 1)]
  for p in range(2, MAX_N + 1):
    if isprime[p]:
      for q in range(p + p, MAX_N + 1, p):
        isprime[q] = False
  return isprime

def isPrime(n):
  isPrime._ = isPrime._ or sieve()
  return isPrime._[n]
isPrime._ = None

def is2017like(n):
  return isPrime(n) and isPrime((n + 1) // 2)

# the number of 2017-like numbers less than or equal to n.
def n2017likeUnder(n):
  if n2017likeUnder._ == None:
    lis = [1 if is2017like(i) else 0 for i in range(MAX_N + 1)]
    n2017likeUnder._ = list(accumulate(lis))
  return n2017likeUnder._[n]
n2017likeUnder._ = None

# the number of 2017-like numbers between l and r.
def solve(l, r):
  return n2017likeUnder(r) - n2017likeUnder(l - 1)

Q = int(sys.stdin.readline())
for i in range(Q):
  l, r = [int(w) for w in sys.stdin.readline().split()]
  print(solve(l, r))
### 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)]


def make_adjlist_d(n, edges):
    res = [[] for _ in range(n+1)]
    for edge in edges:
        res[edge[0]].append(edge[1])
    return res


def main():
    N, M = NMI()
    edges = [NLI() for _ in range(M)]
    S, T = NMI()
    tree = make_adjlist_d(N, edges)

    tree2 = [[] for _ in range(N+1)]
    for i in range(1, N+1):
        gotos = tree[i]
        for goto in gotos:
            tree2[i] += tree[goto]

    tree3 = [[] for _ in range(N+1)]
    for i in range(1, N+1):
        gotos = tree2[i]
        for goto in gotos:
            tree3[i] += tree[goto]

    tree3 = [set(tr) for tr in tree3]

    stack = deque()
    stack.append(S)
    seen = [-1] * (N+1)
    seen[S] = 0
    while stack:
        now = stack.popleft()
        now_step = seen[now]
        for goto in tree3[now]:
            if seen[goto] >= 0:
                continue

            stack.append(goto)
            seen[goto] = now_step + 1

    print(seen[T])



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)]


def make_adjlist_d(n, edges):
    res = [[] for _ in range(n+1)]
    for edge in edges:
        res[edge[0]].append(edge[1])
    return res


def main():
    N, M = NMI()
    edges = [NLI() for _ in range(M)]
    S, T = NMI()

    tree = [[] for _ in range(3*N+1)]
    for e in edges:
        tree[e[0]].append(e[1]+N)
        tree[e[0]+N].append(e[1]+2*N)
        tree[e[0]+2*N].append(e[1])

    stack = deque()
    stack.append(S)
    seen = [-1] * (3*N+1)
    seen[S] = 0
    while stack:
        now = stack.popleft()
        now_step = seen[now]
        for goto in tree[now]:
            if seen[goto] >= 0:
                continue

            stack.append(goto)
            seen[goto] = now_step + 1

    print(seen[T]//3)



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

import sys

input = sys.stdin.readline
N, M = map(int, input().split())

import bisect

ABs = [[] for _ in range(M + 1)]
for i in range(N):
    A, B = map(int, input().split())
    if A <= M:
        ABs[A].append(B)
for i in range(M + 1):
    ABs[i].sort()

As = 0
for i in range(M + 1):
    if len(ABs[i]) > 0:
        As += ABs[i].pop(-1)
        if i < M:
            for b in ABs[i]:
                bisect.insort_left(ABs[i + 1], b)
print(As)

### FIXED CODE ###

import sys

input = sys.stdin.readline
N, M = map(int, input().split())

import bisect

ABs = [[] for _ in range(M + 1)]
for i in range(N):
    A, B = map(int, input().split())
    if A <= M:
        ABs[A].append(B)
ABs[0].sort()

As = 0
for i in range(M + 1):
    if i > 0:
        for b in ABs[i]:
            bisect.insort_left(ABs[0], b)
    if len(ABs[0]) > 0:
        As += ABs[0].pop()
print(As)