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

n, m = list(map(int, input().split()))
card = list(map(int, input().split()))
data = []
for j in range(m):
    data.append(list(map(int, input().split())))
for i in range(m):
    count = 0
    while int(min(card)) < int(data[i][1]) and int(count) < int(data[i][0]):
        card[card.index(min(card))] = data[i][1]
        count += 1
print(sum(card))

### FIXED CODE ###

import bisect
n,m = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()

data = [None] * m
for i in range(m):
    data[i] = list(map(int, input().split()))
data.sort(key = lambda x : x[1])
data.reverse()


ans = 0
count = 0
pre_index = 0
#print(data)
#print(a)
#data[][1] = value, data[][0] = card_num
for i in range(m):
    insert_index = bisect.bisect_left(a, data[i][1])
    count = min(data[i][0], insert_index - pre_index)
    for j in range(pre_index, pre_index + count):
        if j >= n:
            break
        a[j] = 0
    ans += count * data[i][1]
    pre_index = pre_index + count
print(ans + sum(a))
### BUGGY CODE ###

S = input()
Q = int(input())

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

print(S)

### FIXED CODE ###

S = input()
Q = int(input())

num = 0
mae = ""
usi = ""
for i in range(Q):
    tmp = input().split()

    if tmp[0] == "1":
        num += 1

    elif tmp[0] == "2":
        num = num % 2
        dada = num + int(tmp[1])

        if dada == 2:
            usi += tmp[2]
            # S = S + tmp[2]
            # print("usi: "+usi)
        else:
            mae = tmp[2] + mae
            # S = tmp[2] + S
            # print("mae: "+mae)


if num % 2 == 1:
    S = S[::-1]
    S = usi[::-1] + S + mae[::-1]
else:
    S = mae + S + usi
print(S)
### BUGGY CODE ###

n = int(input())
al = list(map(int, input().split()))
al_s = list(set(al))

count = 0
for a in al_s:
    ai = al.count(a) + al.count(a + 1) + al.count(a - 1)
    if ai > count:
        count = ai
print(count)

### FIXED CODE ###

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

ans = [0] * (10 ** 5 + 2)
for a in al:
    ans[a] += 1
    ans[a + 1] += 1
    ans[a - 1] += 1

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

from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix

n, m = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(m)]

graph = [[0] * n for _ in range(n)]
for a, b, c in abc:
    a -= 1
    b -= 1
    graph[a][b] = c
    graph[b][a] = c

dist, pre = floyd_warshall(csr_matrix(graph), return_predecessors=True)

edges = set()
for s in range(n):
    for g in range(s + 1, n):
        while g != s:
            par_g = pre[s][g]
            edge = sorted([par_g, g])
            edge = tuple(edge)
            edges.add(edge)
            g = par_g

ans = m - len(edges)
print(ans)


### FIXED CODE ###

from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix

n, m = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(m)]

graph = [[0] * n for _ in range(n)]
for a, b, c in abc:
    a -= 1
    b -= 1
    graph[a][b] = c
    graph[b][a] = c

dist = floyd_warshall(csr_matrix(graph))

ans = 0
for a, b, c in abc:
    a -= 1
    b -= 1

    if dist[a][b] != c:
        ans += 1

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

#!/usr/bin/env python3
# N,M = map(int,sys.stdin.readline().split())
# a = tuple(map(int,sys.stdin.readline().split())) # single line with multi param
# a = tuple(int(sys.stdin.readline()) for _ in range(N)) # multi line with single param
# a = tuple(tuple(map(int,sys.stdin.readline().rstrip().split())) for _ in range(N)) # multi line with multi param
# s = sys.stdin.readline().rstrip()
# N = int(sys.stdin.readline())
# INF = float("inf")
import sys
from functools import reduce
N,K = map(int,sys.stdin.readline().split())

A = tuple(map(int,sys.stdin.readline().split())) # single line with multi param

cum = [1]*(N+1)
for i in range(N):
    cum[i+1] = cum[i]*A[i]
    if K <= i:
        cur = cum[i+1]//cum[i-K+1]
        #print(i+1,cum[i],i-K,pre,cur)#
        if pre < cur:
            print("Yes")
        else:
            print("No")
        pre = cur
    else:
        pre = cum[i+1]#

#print(cum)
# #https://qiita.com/keymoon/items/11fac5627672a6d6a9f6
# import sys
# T = sys.stdin.readline().rstrip()
# NT = len(T)
# P = sys.stdin.readline().rstrip()
# NP = len(P)
# if NT < NP:
#     exit()
# MOD = 2**61-1 #prime
# A = [ord(c) for c in T]
# pows = [1]*(NT+1)
# shash = [0]*(NT+1)
# for i in range(NT):
#     pows[i+1] = (pows[i]*126)%MOD
#     shash[i+1] = (shash[i]*126+A[i])%MOD
# ph = 0
# for i in range(NP):
#     ph = (ph*126+ord(P[i]))%MOD
# for i in range(len(T)-NP+1):
#     if ph == (shash[i+NP]-shash[i]*pows[NP])%MOD:
#         print(i)

# pre = reduce((lambda x, y: x * y), [A[i] for i in range(K)])
# for i in range(K,N):
#     #print(i)
#     cur = (pre//A[i-K])*A[i]
#     if pre < cur:
#         print("Yes")
#     else:
#         print("No")
#     pre = cur


### FIXED CODE ###

#!/usr/bin/env python3
# N,M = map(int,sys.stdin.readline().split())
# a = tuple(map(int,sys.stdin.readline().split())) # single line with multi param
# a = tuple(int(sys.stdin.readline()) for _ in range(N)) # multi line with single param
# a = tuple(tuple(map(int,sys.stdin.readline().rstrip().split())) for _ in range(N)) # multi line with multi param
# s = sys.stdin.readline().rstrip()
# N = int(sys.stdin.readline())
# INF = float("inf")
import sys
from functools import reduce
N,K = map(int,sys.stdin.readline().split())

A = tuple(map(int,sys.stdin.readline().split())) # single line with multi param

#pre = reduce((lambda x, y: x * y), [A[i] for i in range(K)])
for i in range(K,N):
    #print(i)
    # cur = (pre//A[i-K])*A[i]
    if A[i-K] < A[i]:
        print("Yes")
    else:
        print("No")
    # if pre < cur:
    #
    # else:
    #     print("No")
    # pre = cur
### BUGGY CODE ###

from collections import deque
n,m = map(int,input().split())
g = [[] for _ in range(n)]
for _ in range(m):
    a,b = map(int,input().split())
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)
    
def bfs(u):
    queue = deque([u])
    d = [None]*n    
    d[u] = 0
    while queue:
        v=queue.popleft()
        for i in g[v]:
            if d[i] is None:
                p[i] = v
                queue.append(i)
    return d

p = [None]*n
d = bfs(0)
print('Yes')
        
for q in range(1,n):
    print(int(p[q])+1)

### FIXED CODE ###

from collections import deque
n,m = map(int,input().split())
g = [[] for _ in range(n)]
for _ in range(m):
    a,b = map(int,input().split())
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)
    
def bfs(u):
    queue = deque([u])
    d = [None]*n
    
    d[u] = 0
    while queue:
        v=queue.popleft()
        for i in g[v]:
            if d[i] is None:
                d[i] = d[v]+1
                p[i] = v
                queue.append(i)
    return d

p = [None]*n
d = bfs(0)
if None in p[1:]:
    print('No')
else:
    print('Yes')
        
for q in range(1,len(p)):
    print(int(p[q])+1)
### BUGGY CODE ###

def DFS(list,s,t):
    if s==t:
        return True
    if not t in s:
        return False

    for i in list:
        if(DFS(list,s,t+i)): return True

list=["dream","dreamer","erase","eraser"]
s=input()
t=""
if DFS(list,s,t):
    print("YES")
else: print("NO")

### FIXED CODE ###

import sys
def DFS(list,s,t):
    if s==t:
        print("YES")
        sys.exit()
    if not t in s:
        return

    for i in list:
        DFS(list,s,t+i)

list=["maerd","remaerd","esare","resare"]
s=input()
s=s[::-1]
t=""
c_index=0
while True:
    moto=c_index
    for i in list:
        index=0
        temp=c_index
        while index<len(i) and temp<len(s) and i[index]==s[temp]:
            index+=1
            temp+=1
        if index==len(i):
            c_index=temp
            break
    if c_index==len(s):
        print("YES")
        sys.exit()
    if c_index==moto:
        print("NO")
        sys.exit()
### BUGGY CODE ###

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

R = 0
L = []
for i in range(N):
    if i == 0:
        L = [True] * N
    else:
        L = [L[j] and L[j+1] and A[j]^A[i+j] == A[j]+A[i+j] for j in range(N-i)] + [False]*(i) 
    R += sum(L)
    if sum(L) == 0:
        break
print(R)

### FIXED CODE ###

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

result = 0
Right = 0
tmp = 0
for left in range(N):
    for right in range(Right,N):
        if not (tmp & A[right]):
            tmp += A[right]
            Right = right + 1
        else:
            break
    tmp -= A[left]
    result += Right - left
print(result)
### BUGGY CODE ###

import math
K = int(input())
t = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            t += math.gcd(math.gcd(i,j),k)
print(t)

### FIXED CODE ###

import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
K = int(input())
t = 0
t1 = 0
t2 = 0
for i in range(1,K+1):
    for j in range(i,K+1):
        for l in range(j,K+1):
            if i == j == l:
                t = t + i
            elif i == j or j ==l or l ==i:
                t = t + 3 * gcd(i, j,l)
            else:
                t = t + 6 * gcd(i,j,l)
             
           
           
print(t)
### BUGGY CODE ###

n = int(input())
a = []
a = list(map(int, input().split()))
total = 1
for num in a:
    total *= num
if total > 10**18:
    total = -1

print(total)

### FIXED CODE ###

n = int(input())
a = []
a = list(map(int, input().split()))
total = 1
if 0 in a:
    total = 0
else:
    for num in a:
        total *= num
        if total > 10**18:
            total = -1
            break

print(total)
### BUGGY CODE ###

K = int(input())
S = input()
sum = 0
for i in range(K-2):
  for j in range(i+1, K-1):
    if S[i] == S[j]:
      pass
    else:
      for k in range(j+1, K):
        if S[j] != S[k] and S[k] != S[i] and j - i != k - j:
          sum += 1
print(sum)

### FIXED CODE ###

N = int(input())
S = input()
sum = 0
for i in range(N):
  if S[i] == "R":
    sum += S[i+1:].count("G")*S[i+1:].count("B")
  elif S[i] == "G":
    sum += S[i+1:].count("R")*S[i+1:].count("B")
  elif S[i] == "B":
    sum += S[i+1:].count("G")*S[i+1:].count("R")
for a in range(N):
  for b in range(a+1, N):
    if S[a] == S[b] or 2*b-a >= N:
      pass
    elif S[b] != S[2*b-a] and S[2*b-a] != S[a]:
      sum -= 1
print(sum)
### BUGGY CODE ###

#TIME : 
import sys
from math import sqrt
input = sys.stdin.readline

def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
def invr():
    return(map(int,input().split()))

MAX = 10**18
n = inp()
a = inlt()
if 0 in a:
    print(0)
    exit()
res = 1
for num in a:
    res*=num
if res > MAX:
    print(-1)
else:
    print(res)

### FIXED CODE ###

def main ():
  N = int(input())
  A = list(map(int,input(). split ()))
  if 0 in A:
      print(0)
      return

  prod = 1
  for a in A:
      prod *= a
      if prod > 1000000000000000000:
          print(-1)
          return
  print(prod)

main ()
### BUGGY CODE ###

#!/usr/bin/env python3
INF = 10**7
import sys
input = sys.stdin.readline
from collections import*

def bfs():
    q = deque([(sx, sy)])
    dist = [[INF]*W for _ in range(H)]
    dist[sx][sy] = 0
    
    while q:
        x, y = q.popleft()
        b = dist[x][y]
        if (x, y) == (gx, gy): return dist[gx][gy]
        for nx, ny in [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]:
            if 0<=nx<H and 0<=ny<W and dist[nx][ny] > dist[x][y]:
                if c[nx][ny] == ".":
                    dist[nx][ny] = dist[x][y]
                    q.appendleft((nx, ny))
        for nx in range(x-2, x+3):
            for ny in range(y-2, y+3):
                if 0<=nx<H and 0<=ny<W and dist[nx][ny] > dist[x][y]:
                    if c[nx][ny] == ".":
                        dist[nx][ny] = dist[x][y] + 1
                        q.append((nx, ny)) 
    return -1
H, W = map(int, input().split())

cx, cy = map(int, input().split()); sx, sy = cx - 1, cy - 1
dx, dy = map(int, input().split()); gx, gy = dx - 1, dy - 1
c = [input()[:-1] for _ in range(H)]
ans = bfs()
print(ans if ans != INF else -1)


### FIXED CODE ###

#!/usr/bin/env python3
INF = 10**7
import sys
input = sys.stdin.readline
from collections import*

def bfs():
    q = deque([(sx, sy)])
    dist = [[INF] * W for _ in range(H)]
    dist[sx][sy] = 0
    
    while q:
        x, y = q.popleft()
        b = dist[x][y]
        if (x, y) == (gx, gy):
            return b
        for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            nx, ny = x + dx, y + dy
            if 0 <= nx < H and 0 <= ny < W and dist[nx][ny] > b and c[nx][ny] == ".":
                dist[nx][ny] = b
                q.appendleft((nx, ny))
        B = b + 1
        for dx in range(-2, 3):
            for dy in range(-2, 3):
                nx, ny = x + dx, y + dy
                if 0 <= nx < H and 0 <= ny < W and dist[nx][ny] > B and c[nx][ny] == ".":
                    dist[nx][ny] = B
                    q.append((nx, ny)) 
    return -1

H, W = map(int, input().split())
cx, cy = map(int, input().split())
dx, dy = map(int, input().split())
sx, sy, gx, gy = cx - 1, cy - 1, dx - 1, dy - 1
c = [input()[:-1] for _ in range(H)]
ans = bfs()
print(ans if ans != INF else -1)
### BUGGY CODE ###

import itertools
P=10**9+7
N=int(input())
S=list(itertools.permutations(range(N-1),N-1))
scoresum=0
for sg in S:
    score=0
    L=[0 for i in range(N)]
    W=N
    for i in range(N-1):
        if L[sg[i]]==0:
            W-=1
        L[sg[i]]=1
        if L[sg[i]+1]==0:
            W-=1
        L[sg[i]+1]=1
        if W==0:
            score=i+1
            break
    scoresum+=score
print(scoresum)


### FIXED CODE ###

P=10**9+7
def egcd(a, b):
    (x, lastx) = (0, 1)
    (y, lasty) = (1, 0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return (lastx, lasty, a)
def inv(x):
    return egcd(x,P)[0]
N=int(input())
Fact=[0 for i in range(N+1)]
Finv=[0 for i in range(N+1)]
Fact[0]=1
Finv[0]=1
for i in range(N):
    Fact[i+1]=((i+1)*Fact[i])%P
    Finv[i+1]=(Finv[i]*inv(i+1))%P
SGN=[0 for i in range(N)]
ans=0
for k in range(N):
    if 2*k-N>=0:
        SGN[k]=(((Fact[k-1]*Fact[k])%P)*Finv[2*k-N])%P
        ans=(ans+k*(SGN[k]-SGN[k-1]))%P
print(ans)
### BUGGY CODE ###

import itertools
N,M = map(int,input().split())
A = []
X = [[0]]*M

for m in range(M):
  a,b = map(int,input().split())
  A.append([a,b])
  #X[m] = X[m] + [a] + [b]

ans = []
XX = []
X = A.copy()
for _ in range(N):
  for x,v in itertools.product(X,A):   
    if x[-1] == v[0]:
      if v[1] in x:
        ans.append(x+[v[1]])
        break
      else:
        XX.append(x + [v[1]])
    X = XX

if ans == []:
  print(-1)
else:
  ans[0] = [len(ans[0])-1] + ans[0]
  print(*ans[0][:-1])


### FIXED CODE ###

def main():
  from collections import deque
  n,m,*t=map(int,open(0).read().split())
  i=[0]*n
  o=[[]for _ in range(n)]
  for a,b in zip(*[iter(t)]*2):
    o[a-1]+=b-1,
    i[b-1]+=1
  q=deque(v for v,f in enumerate(i)if f<1)
  r=[]
  while q:
    v=q.popleft()
    r+=v,
    for w in o[v]:
      i[w]-=1
      if i[w]==0:q+=w,
  print(-(len(r)==n))
main()
### BUGGY CODE ###

if __name__ == '__main__':
    N = int(input())
    S = input()

    min = N
    for i in range(N):
        score = S[0:i].count('W') + S[i+1:N].count('E')
        if min > score:
            min = score

    print(min)

### FIXED CODE ###

if __name__ == '__main__':
    N = int(input())
    S = input()

    old = S[1:].count('E')
    ans = old

    for i in range(1, N):
        if S[i-1] == 'W' and S[i] == 'W':
            new = old + 1
        elif S[i-1] == 'E' and S[i] == 'E':
            new = old - 1
        else:
            new = old

        ans = min(ans, new)
        old = new

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

import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    # emp:0 male:1 female:2
    N = int(input())
    L = [-1] * N

    i = 0
    print(i, flush=True)
    s = str(input())
    if s == 'Vacant':
        print(i, flush=True)
        exit()
    elif s == 'Male':
        L[i] = 1
    else:
        L[i] = 2

    i = N // 2
    print(i, flush=True)
    s = str(input())
    if s == 'Vacant':
        print(i, flush=True)
        sys.exit()
    elif s == 'Male':
        L[i] = 1
    else:
        L[i] = 2

    # リストAをとってkeyのある添字を返す
    # なかったら-1
    def bin_search(pl, pr):

        while pl <= pr:
            pc = (pl + pr) // 2
            print(pc, flush=True)

            s = str(input())
            if s == 'Vacant':
                print(pc, flush=True)
                sys.exit()
            elif s == 'Male':
                L[pc] = 1
            else:
                L[pc] = 2

            if abs(L[pr] - L[pc]) == 0 and (pr - pc) % 2 == 1:
                pr = pc - 1
            else:
                pl = pc + 1

    if abs(L[N // 2] - L[0]) == 0 and (N // 2 % 2 == 1):
        bin_search(0, N // 2)
    else:
        bin_search(N // 2 + 1, N - 1)


resolve()

### FIXED CODE ###

N = int(input())


def query(i):
    print(i)
    s = input()
    if s == 'Vacant':
        exit()
    return s


root = query(0)


def is_ok(ind):
    # 左ならTrue
    response = query(ind)
    if ind % 2 == 1:
        return root == response
    else:
        return root != response


def bin_search_meguru():
    """
    初期値のng,okを受け取り,is_okを満たす最小(最大)のokを返す
    まずis_okを定義すべし
    ng, okは,とり得る最小の値-1,とり得る最大の値+1
    """

    # 適当にいじる
    ng = -1  # ind=0が条件を満たすこともあるため
    ok = N  # ind=len(a)-1が条件を満たさないこともあるため

    # いじらない
    # okとngのどちらが大きいかわからないことを考慮
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return False


bin_search_meguru()
### BUGGY CODE ###

import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def tami(n, opened, res):
  global ans
  for i in range(1, N+1):
    if str(i) not in opened:
      for x in data[i]:
        tami(x, opened + " ".join(map(str, KeyID[x])), res + Value[x])
      break
  else:
    ans = min(ans, res)

N, M = IL()

KeyID = [[] for _ in range(M+1)]
data = [[] for _ in range(N+1)]
Value = [0 for _ in range(M+1)]
for i in range(1, M+1):
  a, b = IL()
  c = IL()

  KeyID[i].append(c)
  Value[i] = a
  for num in c:
    data[num].append(i)
#print(Value)
#print(KeyID)

ans = MAX_INT
for i in range(1, N+1):
  tami(i, "", 0)
if ans == MAX_INT:
  print(-1)
else:
  print(ans)

### FIXED CODE ###

MAX_INT = int(10e10)

N, M = list(map(int,input().split()))
Val = [0 for _ in range(M)]
Key = [0 for _ in range(M)]
for i in range(M):
  v, b = list(map(int,input().split()))
  k =  list(map(int,input().split()))
  Val[i] = v
  tmp = 0
  for num in k:
    tmp += 2**(num-1)
  Key[i] = tmp
#print(Val)
#print(Key)

dp = [MAX_INT for _ in range(1<<N)]
dp[0] = 0
for i in range(1<<N):
  for x in range(M):
    #print(i, i|Key[x])
    dp[i|Key[x]] = min(dp[i|Key[x]], dp[i] + Val[x])
#print(dp)
if dp[-1] == MAX_INT:
  print(-1)
else:
  print(dp[-1])
### BUGGY CODE ###

ans = 0
s = list(input())
for i in range(len(s)-2):
    for j in range(i+4,len(s)+1):
      d = s[i:j]
      l = ''.join(d)
      if int(l) % 2019 == 0:
            ans += 1
print(ans)

### FIXED CODE ###

S = input()
cnt = [0]*2019
cnt[0] = 1
n = 0
t = 1
for i in reversed(S):
    n += int(i)*t
    n %= 2019
    cnt[n] += 1
    t *= 10
    t %= 2019
print(sum(i*(i-1)//2 for i in cnt))
### BUGGY CODE ###

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

ans = 10**18
for i in range(N - K + 1):
    p = x[i:i+K]
    p1 = x[i]
    p2 = x[i+K-1]
    lr = abs(p1) + abs(p2 - p1)
    rl = abs(p2) + abs(p1 - p2)
    if lr < rl:
        ans = min(ans,lr)
    else:
        ans = min(ans,rl)
print(ans)


### FIXED CODE ###

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

ans = 10**18
for i in range(N - K + 1):
    p1 = x[i]
    p2 = x[i+K-1]
    lr = abs(p1) + abs(p2 - p1)
    rl = abs(p2) + abs(p1 - p2)
    if lr < rl:
        ans = min(ans,lr)
    else:
        ans = min(ans,rl)
print(ans)
### BUGGY CODE ###

K = int(input())
a = ""
ans = 0

if K%2 == 0:
    print(-1)
elif K%5 == 0:
    print(-1)

else:
    while True:
        a += "7"
        ans += 1
        if int(a)%K == 0:
            break
    print(ans)

### FIXED CODE ###

K = int(input())
a = ""
ans = 0
amari = 10

if K%2 == 0:
    print(-1)
elif K%5 == 0:
    print(-1)

else:
    if K%7 == 0:
        L = 9*K//7
    else:
        L = 9*K

    for p in range(10**6):
        if amari % L == 1:
            print(p+1)
            break
        else:
            amari = (amari*10)%L
### BUGGY CODE ###

N = int(input())
A = list([int(x) for x in input().split()])

A.sort(reverse=True)


comfort = 0

comfort_list = []

for a in A:
    if comfort_list == []:
        comfort_list.append(a)
    else:
        max_comfort = comfort_list.pop(0)
        comfort += max_comfort
        comfort_list.append(min(a, max_comfort))
        comfort_list.append(min(a, max_comfort))

print(comfort)


### FIXED CODE ###

from collections import deque

N = int(input())
A = list([int(x) for x in input().split()])

A.sort(reverse=True)

comfort = 0
comfort_list = deque()

for a in A:
    if len(comfort_list) == 0:
        comfort_list.append(a)
    else:
        max_comfort = comfort_list.popleft()
        comfort += max_comfort
        comfort_list.append(min(a, max_comfort))
        comfort_list.append(min(a, max_comfort))

print(comfort)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
ans = [0] * n
for i in range(n):
  f = True
  b = a[i:]+a[:i]
  for j in b:
    if f:
      ans[i] += j
      f = False
    else:
      ans[i] -= j
      f = True
print(*ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
ans = [0] * n
ans[0] = sum(a[::2]) - sum(a[1::2])
for i in range(1,n):
  ans[i] = a[i-1] * 2 - ans[i-1]
print(*ans)
### BUGGY CODE ###

import sys, collections

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    son = [[] for i in range(N)]
    for i in range(M):
        u, v = map(int, input().split())
        son[u-1].append(v-1)
    S, T = map(int, input().split())

    graphB = [[] for i in range(N)] 
    for i in range(N):
        for s1 in son[i]:
            for s2 in son[s1]:
                for s3 in son[s2]: graphB[i].append(s3)
    
    q = collections.deque()
    visited = [-1] * N
    q.append((0, S-1))
    while q:
        cost, now = q.popleft()
        if visited[now] == -1:
            visited[now] = cost
            for s in graphB[now]:
                if visited[s] == -1: q.append((cost + 1, s))

    print(visited[T-1])
    return 0

if __name__ == "__main__":
    solve()

### FIXED CODE ###

import sys, collections

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    son = [[] for i in range(N)] 
    for i in range(M):
        u, v = map(int, input().split())
        son[u-1].append(v-1)
    S, T = map(int, input().split())

    dist = [[-1, -1, -1] for i in range(N)]
    que = collections.deque()
    que.append((0, S-1, 0)) #k回目の移動, 場所, mod
    while que:
        cost, now, mod = que.popleft()
        if dist[now][mod] == -1:
            dist[now][mod] = cost
            if mod == 0:
                for s in son[now]:
                    if dist[s][1] == -1: que.append((cost + 1, s, 1))
            else:
                for s in son[now]:
                    if dist[s][(mod + 1) % 3] == -1: que.append((cost, s, (mod + 1) % 3))

    print(dist[T-1][0])  
    return 0

if __name__ == "__main__":
    solve()