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

# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


# -------------------------------------------------------------
# main
# -------------------------------------------------------------
S = input().rstrip()
T = input().rstrip()

chars = set()
for t in T:
    chars.add(t)

for c in chars:
    if S.find(c) == -1:
        print(-1)
        sys.exit()

idx = S.index(T[0])
ans = idx+1
for i in range(1,len(T)):
    S = S[idx+1:] + S[:idx+1]
    idx = S.index(T[i])
    ans += idx+1

print(ans)

### FIXED CODE ###

#!/usr/bin/env python3
#ABC138 E

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = input()
t = input()
n = len(s)
m = len(t)
f = [False]*26
f2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(n):
    f[alpha.index(s[i])] = True
for i in range(m):
    f2[alpha.index(t[i])] = True
for i in range(26):
    if not f[i] and f2[i]:
        print(-1)
        quit()
ans = 0
inde = s.index(t[0])
ans += inde+1
for i in range(1,m):
    s = s[inde+1:] + s[:inde+1]
    inde = s.index(t[i])
    ans += inde+1
print(ans)
### BUGGY CODE ###

n,k=map(int,input().split())
l=list(map(int,input().split()))

ll=[]
for i in range(n):
    ll.append((l[i]+1)*l[i]/2/l[i])

lll=[]
for i in range(n-k+1):
    lll.append(sum(ll[i:i+k]))

print(max(lll))


### FIXED CODE ###


n,k=map(int,input().split())
l=list(map(int,input().split()))

ll=[]
for i in range(n):
    ll.append((l[i]+1)/2)

lll=[]
for i in range(n-k+1):
    if i==0:
        lll.append(sum(ll[0:k]))
    else:
        lll.append(lll[i-1]+ll[i+k-1]-ll[i-1])

print(max(lll))
### BUGGY CODE ###

#coding: UTF-8
import math
N = int(input())
#print(N)

for i in range(1,N+1):
  if i <=5:
    print(0)
    continue
  cnt = 0
  m = int(math.sqrt(i))
  #平方根までやればよい
  for x in range(1,m):
    for y in range(1,m):
      for z in range(1,m):
        a = (x+y+z)**2 -x*y -y*z - z*x
        if a == i:
          cnt += 1
  print(cnt)

### FIXED CODE ###

#coding: UTF-8
import math
N = int(input())
#print(N)

ans = [0]*10001

for x in range(1,101):
  for y in range(1,101):
    for z in range(1,101):
      a = x**2+y**2+z**2+x*y+y*z+z*x
      if a <=10000:
        ans[a] += 1
    
for i in range(1,N+1):
  print(ans[i])
### BUGGY CODE ###

# 後だし

import collections

H, W = map(int, input().split())
C = tuple((int(x)-1 for x in input().split()))
D = tuple((int(x)-1 for x in input().split()))
S = list()
for _ in range(H):
    S.append(list(input()))

queue = collections.deque()
visited = collections.deque()
queue.append(C)
cy, cx = C
S[cy][cx] = 0

while len(queue):
    while len(queue):
        y, x = queue.popleft()
        visited.append((y, x))
        cost = S[y][x]
        for i, j in [(y-1, x), (y+1, x), (y, x-1), (y, x+1)]:
            if not (0 <= i < H and 0 <= j < W):
                continue
            if S[i][j] == '.':
                S[i][j] = cost
                queue.append((i, j))

    while len(visited):
        y, x = visited.popleft()
        cost = S[y][x]
        for i in range(y-2, y+2+1):
            for j in range(x-2, x+2+1):
                if not (0 <= i < H and 0 <= j < W):
                    continue
                if S[i][j] == '.':
                    S[i][j] = cost+1
                    queue.append((i, j))

dy, dx = D
result = S[dy][dx]
if result == '.':
    print(-1)
else:
    print(result)


### FIXED CODE ###

# 後だし

import collections

H, W = map(int, input().split())
C = tuple((int(x)-1 for x in input().split()))
D = tuple((int(x)-1 for x in input().split()))
S = list()
for _ in range(H):
    S.append(list(input()))

queue = collections.deque()
visited = collections.deque()
queue.append(C)
cy, cx = C
S[cy][cx] = 0

while len(queue):
    while len(queue):
        y, x = queue.popleft()
        visited.append((y, x))
        cost = S[y][x]
        for i, j in [(y-1, x), (y+1, x), (y, x-1), (y, x+1)]:
            if not (0 <= i < H and 0 <= j < W):
                continue
            if S[i][j] == '.':
                S[i][j] = cost
                queue.append((i, j))

    while len(visited):
        y, x = visited.popleft()
        cost = S[y][x]
        for i in range(y-2, y+2+1):
            for j in range(x-2, x+2+1):
                if not (0 <= i < H and 0 <= j < W):
                    continue
                if S[i][j] == '.':
                    S[i][j] = cost+1
                    queue.append((i, j))

dy, dx = D
result = S[dy][dx]
if result == '.':
    print(-1)
else:
    print(result)
### BUGGY CODE ###

#!/usr/bin/env python3
import sys

def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    l = [0]*X*Y
    for i in range(X):
        for j in range(Y):
            l[i+j*X] = A[i]+B[j]

    l.sort()
    t = max(3000, K)
    l = l[-t:]

    ans = [0] * len(l)*Z
    ls = len(l)
    for i in range(ls):
        for j in range(Z):
            ans[i+j*ls] = l[i] + C[j]

    ans.sort(reverse=True)
    for i in range(K):
        print(ans[i])


    return


# Generated by 1.1.3 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    Y = int(next(tokens))  # type: int
    Z = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(X) ]  # type: "List[int]"
    B = [ int(next(tokens)) for _ in range(Y) ]  # type: "List[int]"
    C = [ int(next(tokens)) for _ in range(Z) ]  # type: "List[int]"
    solve(X, Y, Z, K, A, B, C)

if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys

def solve(X: int, Y: int, Z: int, K: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    l = [0]*X*Y
    for i in range(X):
        for j in range(Y):
            l[i+j*X] = A[i]+B[j]

    l.sort(reverse=True)
    t = min(3000,X*Y)
    l = l[:t]

    ans = [0] * (t) * Z
    for i in range(t):
        for j in range(Z):
            ans[i+j*t] = l[i] + C[j]

    ans.sort(reverse=True)
    for i in range(K):
        print(ans[i])


    return


# Generated by 1.1.3 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    Y = int(next(tokens))  # type: int
    Z = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(X) ]  # type: "List[int]"
    B = [ int(next(tokens)) for _ in range(Y) ]  # type: "List[int]"
    C = [ int(next(tokens)) for _ in range(Z) ]  # type: "List[int]"
    solve(X, Y, Z, K, A, B, C)

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

n = int(input())
li = list(map(int, input().split()))
ans = 1
for e in li:
    ans *= e

if ans > 1e18:
    print(-1)
else:
    print(ans) 

### FIXED CODE ###

n = int(input())
li = list(map(int, input().split()))
if 0 in li:
    print(0)
    exit()
ans = 1
for e in li:
    ans *= e
    if ans > 10**18:
        print(-1)
        exit()

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

def main():
    """"ここに今までのコード"""
    N, K = map(int, input().split())
    dice_list = list(map(int, input().split()))
    expected_values = [sum(range(1, i + 1)) / i for i in dice_list]
    acc = [0 for i in range(N+1)]
    for i in range(1, N+1):
        acc[i] = acc[i-1] + expected_values[i-1]
    
    max_sum = -1

    for i in range(N - K + 1):
        s = acc[i + K] - acc[i]
        if s > max_sum:
            max_sum = s
    
    print(max_sum)

if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    """"ここに今までのコード"""
    N, K = map(int, input().split())
    dice_list = list(map(int, input().split()))
    max, s = 0, 0

    for i in range(K):
        s += (dice_list[i] + 1) / 2
    
    max = s

    for i in range(N-K):
        s -= (dice_list[i] + 1) / 2
        s += (dice_list[i+K] + 1) / 2
        if s > max:
            max = s
    
    print(max)


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

import sys

INF = float('inf')

input = sys.stdin.readline


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


N, K = LIST()
h = LIST()

h.extend([0] * K)

# DP配列を初期化
dp = [INF] * (N + K)

# 初期条件を入力
dp[0] = 0

# テーブルを更新 くばるDP
for i in range(0, N):
    for j in range(1, K + 1):
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]))

print(dp[N - 1])


### FIXED CODE ###

import sys

INF = float('inf')

input = sys.stdin.readline


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


N, K = LIST()
h = LIST()

# DP配列を初期化
dp = [INF] * N

# 初期条件を入力
dp[0] = 0

# テーブルを更新 もらうDP
for i in range(1, N):
    x = max(0, i - K)
    dp[i] = min([dp_ + abs(h[i] - h_) for dp_, h_ in zip(dp[x:i], h[x:i])])

print(dp[N - 1])
### BUGGY CODE ###

def gcd(a,b):
    if a>b:
        r=a%b
        if r==0:
            return b
        return gcd(b,r)
    elif a<b:
        r=b%a
        if r==0:
            return a
        return gcd(a,b%a)
    else:
        return a
k=int(input())
sum=0
for i in range(1,k+1):
    for j in range(1,k+1):
        for l in range(1,k+1):
            sum+=gcd(i,gcd(j,l))
print(sum)


### FIXED CODE ###

def gcd(a,b):
    if a>b:
        r=a%b
        if r==0:
            return b
        return gcd(b,r)
    elif a<b:
        r=b%a
        if r==0:
            return a
        return gcd(a,b%a)
    else:
        return a
k=int(input())
sum=0
for i in range(1,k+1):
    for j in range(1,k+1):
        for l in range(1,k+1):
            sum+=gcd(i,gcd(j,l))
print(sum)
### BUGGY CODE ###

n,m = map(int,input().split())
ls = [[0,0] for _ in range(m)]
di = [[] for _ in range(n)]
for k in range(m):
    c,d = map(int,input().split())
    ls[k][0],ls[k][1] = c,d
    di[c-1].append(d)
for i in range(n):
    di[i].sort()
for j in range(m):
    a,b = ls[j][0],ls[j][1]
    p = di[a-1].index(b)
    q = 10**12 + (a)*10**6 +(p+1)
    print(str(q)[1:])



### FIXED CODE ###

n,m = map(int,input().split())
ls = [[] for _ in range(n+1)]
di = []
for i in range(m):
    a,b = map(int,input().split())
    ls[a].append([b,i+1])
for j in range(n+1):
    if ls[j] != []:
        ls[j].sort(key=lambda x :x[0])
        for k in range(len(ls[j])):
            di.append([ls[j][k][1],str(10**12+j*10**6+k+1)[1:]])
di.sort(key=lambda x :x[0])
for l in range(m):
    print(di[l][1])
### BUGGY CODE ###

import bisect
import sys
input = sys.stdin.readline

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

for _ in range(m):
    max_price = A.pop(-1) // 2
    next_index = bisect.bisect(A, max_price)
    A.insert(next_index, max_price)

total = sum(A)
print(total)


### FIXED CODE ###

import heapq
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
A = map(int, input().split())
A = list(map(lambda x: x*(-1), A))
heapq.heapify(A)

for _ in range(m):
    max_price = -heapq.heappop(A) // 2
    heapq.heappush(A, -max_price)

total = -sum(A)
print(total)
### BUGGY CODE ###

# C - Go to School
n = (int)(input())

a = list(map(int, input().split()))
map = {}

for i in range(n):
    map[a[i]] = i + 1

a.sort()

result = ""
for j in a:
    result = result + str(map[j]) + " "

print(result[:-1])

### FIXED CODE ###

# C - Go to School
n = (int)(input())

a = list(map(int, input().split()))
map = {}

for i in range(n):
    map[a[i]] = i + 1

a.sort()

result = ""
for j in a:
    print(map[j], end="")
    if len(a) != j:
        print(' ', end="")
### BUGGY CODE ###

from itertools import combinations
import math
N=int(input())
l1=list(map(int,input().split()))
max_n=max(l1)
l2=tuple(combinations(l1,2))
l3=[sum(i) for i in l2 if sum(i)<=max_n]
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
ans=comb(N,3)
for i in l3:
  for j in l1:
    if i<=j:
      ans-=1
print(ans)

### FIXED CODE ###

import bisect
N=int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
for i in range(1,N-1):
  for j in range(i+1,N):
    m=bisect.bisect_right(l,l[j]-l[i])
    if i>m:
      ans+=i-m
print(ans)
### BUGGY CODE ###

from collections import Counter
def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, m, r = read_line(int, int, int)

rs = [int(r) for r in input().split()]

cities = Counter()

routes = {}
costs = {}
for _ in range(m):
    a, b, c = read_line(int, int, int)

    cities[a] = 1
    cities[b] = 1

    if a in routes:
        routes[a][b] = c
    else:
        routes[a] = { b : c }

    if b in routes:
        routes[b][a] = c
    else:
        routes[b] = { a : c }

for a in cities.keys():
    for b in cities.keys():
        if a not in costs:
            costs[a] = {}
        if b not in costs[a]:
            if a in routes and b in routes[a]:
                costs[a][b] = routes[a][b]
            else:
                costs[a][b] = 100000 + 1

for k in cities.keys():
    for a in cities.keys():
        for b in cities.keys():
            if costs[a][b] > costs[a][k] + costs[k][b]:
                costs[a][b] = costs[a][k] + costs[k][b]

def cost(selected):
    c = 0
    for i in range(len(selected) - 1):
        a = selected[i]
        b = selected[i + 1]
        c += costs[a][b]
    return c

smallest = 100000 + 1

def passing_all_cities(selected):
    for r in rs:
        if r not in selected:
            return False
    return True

def f(selected, rest):
    global smallest
    if len(rest) == 0 or passing_all_cities(selected):
        c = cost(selected)
        if smallest > c:
            smallest = c
    else:
        for i in range(len(rest)):
            r = rest[i]
            rest_copy = rest[:]
            del rest_copy[i]
            # if cost(selected + [r]) < smallest:
            #     f(selected + [r], rest_copy)
            f(selected + [r], rest_copy)

f([], list(cities.keys()))
print(smallest)


### FIXED CODE ###

from itertools import permutations

def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, m, r = read_line(int, int, int)

rs = [int(r) for r in input().split()]

costs = {}
for i in range(n + 1):
    costs[i] = {}
    for j in range(n + 1):
        costs[i][j] = float('inf')

for _ in range(m):
    a, b, c = read_line(int, int, int)

    costs[a][b] = costs[b][a] = c

for k in range(n + 1):
    for i in range(n + 1):
        for j in range(n + 1):
            c = costs[i][k] + costs[k][j]
            if costs[i][j] > c:
                costs[i][j] = c

def cost(selected):
    c = 0
    for i in range(len(selected) - 1):
        a = selected[i]
        b = selected[i + 1]
        c += costs[a][b]
    return c

smallest = float('inf')

for p in permutations(rs):
    c = cost(p)
    if smallest > c:
        smallest = c

print(smallest)
### BUGGY CODE ###

N, W = map(int, input().split())
wv = [map(int, input().split()) for i in range(N)]
dp = [[0] * (W+1) for _ in range(N+1)]
for i, (w, v) in enumerate(wv):
    for j in range(W+1):
        dp[i+1][j] = max(dp[i+1][j], dp[i][j])
        if j-w >= 0:
            dp[i+1][j] = max(dp[i+1][j], dp[i][j-w] + v)
print(dp[N][W])

### FIXED CODE ###

import numpy as np
N, W = map(int, input().split())
wv = [map(int, input().split()) for i in range(N)]
dp = np.zeros((N+1, W+1), dtype = np.int)
for i, (w, v) in enumerate(wv):
    dp[i+1] = np.maximum(dp[i+1], dp[i])
    dp[i+1][w:] = np.maximum(dp[i+1][w:], dp[i][:-w] + v)
print(dp[N][W])
### BUGGY CODE ###

from collections import deque
def bfs():
    d = [[float("inf") for i in range(W)] for j in range(H)]
    dx = [1,-1,0,0]
    dy = [0,0,1,-1]
    que = deque([])
    ans = 0

    for i in range(W):
        for j in range(H):
            if S[j][i] == "#":
                d[j][i] = 0
                que.append((i,j))

    while que:
        p = que.popleft()
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if 0<=nx<W and 0<=ny<H and S[ny][nx] != "#" and d[ny][nx] == float("inf"):
                que.append((nx,ny))
                d[ny][nx] = d[p[1]][p[0]] + 1
                if d[ny][nx] > ans:
                    ans = d[ny][nx]
    return ans

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

print(bfs())


### FIXED CODE ###

from collections import deque
h,w = map(int,input().split())
grid = []
dq = deque([])
dx = [1,-1,0,0]
dy = [0,0,1,-1]
ans = 0
d = [[-1 for i in range(w)] for j in range(h)]
for i in range(h):
    maze = list(input())
    for j in range(w):
        if maze[j] == "#":
            dq.append((j,i))
            d[i][j] = 0
    grid.append(maze)
while dq:
    x,y = dq.popleft()
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if 0 <= nx <= w-1 and 0 <= ny <= h-1 and grid[ny][nx] != "#" and d[ny][nx] == -1:
            dq.append((nx,ny))
            d[ny][nx] = d[y][x] + 1
            ans = max(ans,d[ny][nx])
print(ans)
### BUGGY CODE ###

N = int(input())
power = 1
a = 10**9 + 7
for i in range(1,N+1,1):
    power = power*i
print(power%a)


### FIXED CODE ###

N = int(input())
ans = 1
for i in range(1,N+1,1):
    ans = (ans*i)%(10**9+7)
print(ans)
### BUGGY CODE ###

import math
n = int(input())
cc = [0 for i in range(n-1)]
ss = [0 for i in range(n-1)]
ff = [0 for i in range(n-1)]
for i in range(n-1):
    c, s, f = list(map(int, input().split()))
    cc[i] = c
    ss[i] = s
    ff[i] = f
for l in range(n-1):
    ans = ss[l]+cc[l]
    for k in range(l+1, n-1):
        temp = ss[k]
        while temp < ans:
            temp += ff[k]
        ans = temp + cc[k]
    print(ans)
print(0)

### FIXED CODE ###

n = int(input())
cc = []
ss = []
ff = []
for i in range(n-1):
    c, s, f = list(map(int, input().split()))
    cc.append(c)
    ss.append(s)
    ff.append(f)
for l in range(n-1):
    ans = ss[l]+cc[l]
    for k in range(l+1, n-1):
        temp = ss[k]
        while temp < ans:
            temp += ff[k]
        ans = temp + cc[k]
    print(ans)
print(0)
### BUGGY CODE ###

n,k=map(int,input().split())

import sys
sys.setrecursionlimit(2147483647)

def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p
p = 10**9+7 #割る数
N = n+1  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

ans=1
for i in range(1,k+1):
    ans+=(cmb(n, i, p)*cmb(n-1,n-i-1,p))%p
    
    #print(i,cmb(n, i, p),cmb(n-1,n-i-1,p))

print(ans%p)
    


### FIXED CODE ###

n,k=map(int,input().split())

import sys
sys.setrecursionlimit(2147483647)

def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p
p = 10**9+7 #割る数
N = 2*n+1  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

ans=1

if k>n:
    ans=cmb(n+n-1,n,p)

else:

    for i in range(1,k+1):
        ans+=(cmb(n, i, p)*cmb(n-1,n-i-1,p))%p
        
        #print(i,cmb(n, i, p),cmb(n-1,n-i-1,p))

print(ans%p)
### BUGGY CODE ###

n,m=map(int,input().split())
p = [int(i)-1 for i in input().split() ]


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]
    
    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())



uf = UnionFind(n)
for i in range(m):
    x,y = map(int,input().split())
    uf.union(x-1,y-1)

ans=0
for cand in uf.all_group_members().values():
    ans+=len(set(cand) & set(p[i] for i in cand) )
print(ans)

### FIXED CODE ###

n,m=map(int,input().split())
p = [int(i)-1 for i in input().split() ]


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

from collections import deque

groups=[]
been=[0]*n
for i in range(n):
    if been[i]==0:
        group=[i]
        been[i]=1
        q = deque([(i,togo) for togo in tree[i]]) 
        while q:
            parent,x = q.popleft()
            if been[x]==1:
                continue
            been[x]=1
            group.append(x)
            for y in tree[x]:
                if y == parent:#親ノードを飛ばし
                    continue
                q.append((x,y))#子ノードを追加
        groups.append(group)

ans=0
for group in groups:
    ans+= len( set(group) & set(p[g] for g in group) )

print(ans)