Harryxun/stf_run huggingface dataset:
#!/usr/bin/env python
# encoding: utf-8


class Solution:
    """
    @param prices: Given an integer array
    @return: Maximum profit
    """

    @staticmethod
    def swap(a, b):
        a, b = b, a
        return a, b

    @staticmethod
    def bubble_sort(self):
        # write your code here
        array_length = int(input())
        unsorted_array = [int(x) for x in input().split()]

        flag = 1
        count = 0
        while flag:
            flag = 0
            for j in range(array_length - 1, 0, -1):
                if unsorted_array[j] < unsorted_array[j - 1]:
                    self.swap(unsorted_array[j], unsorted_array[j - 1])
                    flag = 1
                    count += 1
        print(" ".join(map(str, unsorted_array)))
        print(str(count))


if __name__ == '__main__':
    solution = Solution()
    solution.bubble_sort()

### FIXED CODE ###

#!/usr/bin/env python
# encoding: utf-8


class Solution:

    @staticmethod
    def bubble_sort():
        # write your code here
        array_length = int(input())
        array = [int(x) for x in input().split()]

        flag, count, cursor = 1, 0, 0
        while flag:
            flag = 0
            for j in range(array_length - 1, cursor, -1):
                if array[j] < array[j - 1]:
                    array[j], array[j - 1] = array[j - 1], array[j]
                    flag = 1
                    count += 1
        cursor += 1
        print(" ".join(map(str, array)))
        print(str(count))


if __name__ == '__main__':
    solution = Solution()
    solution.bubble_sort()
import sys
n, m=map(int, raw_input().split())
adj=[[] for i in range(n+1)]
for i in range(m):
	u, v=map(int, raw_input().split())
	adj[u].append(v)
	adj[v].append(u)
isolated_cnt=0
bipartite_cnt=0
other_cnt=0
color=[0]*(n+1)
def dfs(cur):
	for to in adj[cur]:
		if not color[to]:
			color[to]=3-color[cur]
			if not dfs(to):
				return False
		else:
			if color[to]!=3-color[cur]:
				return False
	return True
for i in range(1, n+1):
	if adj[i]:
		if not color[i]:
			color[i]=1
			if dfs(i):
				bipartite_cnt+=1
			else:
				other_cnt+=1
	else:
		isolated_cnt+=1
print>>sys.stderr, isolated_cnt, bipartite_cnt, other_cnt
print isolated_cnt*n*2-isolated_cnt*isolated_cnt+bipartite_cnt*(bipartite_cnt*2+other_cnt)+other_cnt*(bipartite_cnt+other_cnt)

### FIXED CODE ###

import sys
n, m=map(int, raw_input().split())
sys.setrecursionlimit(n+1000)
adj=[[] for i in range(n+1)]
for i in range(m):
	u, v=map(int, raw_input().split())
	adj[u].append(v)
	adj[v].append(u)
isolated_cnt=0
bipartite_cnt=0
other_cnt=0
color=[0]*(n+1)
def dfs(cur):
	global ok
	for to in adj[cur]:
		if not color[to]:
			color[to]=3-color[cur]
			dfs(to)
		else:
			if color[to]!=3-color[cur]:
				ok=False
for i in range(1, n+1):
	if adj[i]:
		if not color[i]:
			color[i]=1
			ok=True
			dfs(i)
			if ok:
				bipartite_cnt+=1
			else:
				other_cnt+=1
	else:
		isolated_cnt+=1
print>>sys.stderr, isolated_cnt, bipartite_cnt, other_cnt
print isolated_cnt*n*2-isolated_cnt*isolated_cnt+bipartite_cnt*(bipartite_cnt*2+other_cnt)+other_cnt*(bipartite_cnt+other_cnt)
a,b=map(int,input().split())
print(a*b//euclidean(a,b))

def euclidean(a,b):
    r=a%b
    if r==0:
        return b
    return euclidean(b,r)

### FIXED CODE ###

def euclidean(a,b):
    while True:
      r=a%b
      if r==0:
         return b
      a,b=b,r

a,b=map(int,input().split())
print(a*b//euclidean(a,b))
W,H,x,y,r=map(int,input().split())
if 0 < x + r < W:
????if 0 < y + r < H:
????????print("Yes")
????else:
????????Print("No")
else:
????print("No")

### FIXED CODE ###

W,H,x,y,r=map(int,input().split())
if r <= x and r <= y and x+r <= W and y+r <= H:
  print("Yes")
else:
  print("No")
import sys

def main():
  n, k, *h = map(int, sys.stdin.read().split())
  dp = [float("inf")] * n
  dp[0] = 0
  for i in range(1, n):
    if i < k:
      dp[i] = min([ dpj+abs(hj-h[i]) for dpj, hj in zip(dp[:i], h[:i]) ])
    else:
      dp[i] = min( [ dpj+abs(hj-h[i]) for dpj, hj in zip(dp[i-k:k], h[i-k:k]) ] )
  print(dp[n-1])
  
if __name__ == "__main__":
  main()

### FIXED CODE ###

n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [float("inf")] * n
dp[0] = 0
for i in range(1, n):
  if i < k:
    dp[i] = min([ dp[j]+abs(h[j]-h[i]) for j in range(i) ])
  else:
    dp[i] = min( [ dp[i-j-1]+abs(h[i-j-1]-h[i]) for j in range(k) ] )
print(dp[n-1])
import itertools
import heapq
import sys
import functools
import operator
import numpy as np
input = sys.stdin.readline
#-------------
n,k = map(int,input().split())
a = list(map(int,input().split()))
#-------------
a.sort(reverse = True)
w = 10**9 + 7
ans = 1
if k%2 == 1:
  for i in range(k):
    ans *= a[i]
  print(ans % w)

else:
  if n%2 == 0:
    a_new = [a[2*i]*a[2*i+1] for i in range(n//2)]
    a_new.sort(reverse = True)
    for j in range(k//2):
      ans *= a_new[j]
    print(ans % w)
      
  else:
    a_plus = heapq.nlargest(k, a)
    a_minus = heapq.nsmallest(k, a)
    a_plus.sort(reverse = True)
    a_minus.sort()
    a_plus_new = [a_plus[2*i]*a_plus[2*i+1] for i in range(len(a_plus))]
    a_minus_new = [a_minus[2*i]*a_minus[2*i+1] for i in range(len(a_minus))]
    a_new = a_plus_new + a_minus_new
    a_new.sort(revserse = True)
    for j in range(k//2):
      ans *= a_new[j]
    print(ans % w)

### FIXED CODE ###

import sys
 
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
mod = pow(10, 9)+7
zero = 0
plus = []
minus = []
for i in range(n):
  if a[i] == 0:
    zero += 1
  elif a[i] > 0:
    plus.append(a[i])
  else:
    minus.append(a[i])
 
P, M = len(plus), len(minus)
plus.sort(reverse=True)
minus.sort()
ans = 1
if n == k:
  for i in range(n):
    ans *= a[i]
    ans %= mod
elif P+M < k:
  ans = 0
elif P == 0:
  if zero and k%2:
    ans = 0
  else:
    if k%2:
      minus.reverse()
    for i in range(k):
      ans *= minus[i]
      ans %= mod
else:
  q = 0
  if k%2:
    ans *= plus[0]
    ans %= mod
    q = 1
  judge = []
  for i in range((len(plus)-q)//2):
    judge.append(plus[2*i+q]*plus[2*i+q+1])
  for i in range(len(minus)//2):
    judge.append(minus[2*i]*minus[2*i+1])
  judge.sort(reverse=True)
  for i in range(k//2):
    ans *= (judge[i]%mod)
    ans %= mod
 
print(ans)
# -*- coding: utf-8 -*-
from itertools import itertools.combinations_with_replacement
N,M,Q=map(int,input().split())
abcd=[]
for _ in range(Q):
    abcd.append(tuple(map(int,input().split())))
Abase=[]
for a in range(1,M+1):
    Abase=Abase+[a]
A=set(itertools.combinations_with_replacement(Abase,N))
ans=0
for i in A:
    temp=0
    for j in abcd:
        if i[j[1]-1]-i[j[0]-1]==j[2]:
            temp=temp+j[3]
    if temp>=ans:
        ans=temp
print(ans)

### FIXED CODE ###

# -*- coding: utf-8 -*-
import itertools
N,M,Q=map(int,input().split())
abcd=[]
for _ in range(Q):
    abcd.append(tuple(map(int,input().split())))
Abase=[]
for a in range(1,M+1):
    Abase=Abase+[a]
A=set(itertools.combinations_with_replacement(Abase,N))
ans=0
for i in A:
    temp=0
    for j in abcd:
        if i[j[1]-1]-i[j[0]-1]==j[2]:
            temp=temp+j[3]
    if temp>=ans:
        ans=temp
print(ans)
while True:
    M, N = map(int, raw_input().split())
    if M == N == 0: break
    m = [i for i in range(1, M+1)]
    index = 0
    num = 1
    for i in range(N):
        line = raw_input()
        if num % 15 == 0:
            if line != 'FizzBuzz':
                del m[index]
        elif num % 3 == 0:
            if line != 'Fizz':
                del m[index]
        elif num % 5 == 0:
            if line != 'Buzz':
                del m[index]
        else:
            if not line.isdigit():
                del m[index]
            else:
                if int(line) != num:
                    del m[index]
        if len(m) == 1:
            break
        elif index+1 >= len(m):
            index = 0
        else:
            index += 1
        num += 1
    print ' '.join(map(str, m))

### FIXED CODE ###

while True:
    M, N = map(int, raw_input().split())
    if M == N == 0: break
    m = [i for i in range(1, M+1)]
    index = 0
    num = 1
    for i in range(N):
        if len(m) != 1:
            line = raw_input()
            flag = False
            if num % 15 == 0:
                if line != 'FizzBuzz':
                    del m[index]
                    flag = True
            elif num % 3 == 0:
                if line != 'Fizz':
                    del m[index]
                    flag = True
            elif num % 5 == 0:
                if line != 'Buzz':
                    del m[index]
                    flag = True
            else:
                if not line.isdigit():
                    del m[index]
                    flag = True
                else:
                    if int(line) != num:
                        del m[index]
                        flag = True
            if not flag:
                index += 1
            if index+1 > len(m):
                index = 0
            num += 1
        else:
            raw_input()
    else:
        print ' '.join(map(str, m))
n,x,=map(int, input().split())
w_list = list(map(int, input().split()))

d=0
for i in range(n+1):
   d=d+w_list[i]
   if(d>x):
       break
   
print(i+1)

### FIXED CODE ###

n,x = map(int,input().split())
li = list(map(int,input().split()))
 
ans = 1
now = 0
 
for i in li:
    now += i
    if now <= x:
        ans += 1
    else:
        break
 
print(ans)
def main():
  num_r,num_c = map(int,input().split())
  field = [input() for i in range(num_r)]
  q = []
  seen = [0]*(num_c*num_r)
  count = 0

  for i in range(num_r):
    for j in range(num_c):
      if field[i][j] == "#":
        seen[i*num_c+j] = 1
        q.append(i*num_c+j)

  while q:
    tank = []
    for i in q:
      if (0<=i-num_c) and (seen[i-num_c] == 0):
        tank.append(i-num_c)
        seen[i-num_c] = 1
      if (i+num_c<num_c*num_r) and (seen[i+num_c] == 0):
        tank.append(i+num_c)
        seen[i+num_c] = 1
      if (0<=i%num_c -1) and (seen[i-1] == 0):
        tank.append(i-1)
        seen[i-1] = 1
      if (i%num_c+1<num_r) and (seen[i+1] == 0):
        tank.append(i+1)
        seen[i+1] = 1

    count = count + 1
    q = tank

  print(count-1)
  
if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    h,w = map(int,input().split())
    a = []
    for i in range(h):
        a.append(input())

    used = [True]*(h*w)

    p = []
    for i in range(h):
        for j in range(w):
            if a[i][j] == '#':
                p.append(i*w+j)
                used[i*w+j] = False


    
    res = 0
    while p:
        tank = []
        for e in p:
            if e>w-1 and used[e-w]:
                tank.append(e-w)
                used[e-w] = False
            if e%w > 0 and used[e-1]:
                tank.append(e-1)
                used[e-1] = False
            if e<(h-1)*w and used[e+w]:
                tank.append(e+w)
                used[e+w] = False
            if e%w<w-1 and used[e+1]:
                tank.append(e+1)
                used[e+1] =  False
        p = tank
        res += 1
    print(res-1)

if __name__ == '__main__':
    main()
a = int(input())
s = str(input())

if a >= 3200:
    print(s)
else:
    print(red)

### FIXED CODE ###

a = int(input())
s = str(input())

if a >= 3200:
    print(s)
else:
    print('red')
x,y=map(int,input().split())
T=x+y
W=x-T//3
D=10**9
#print(T//3)

import sys
if T%3!=0 or y/x<1/2 or y/x>2:
    print(0)
    sys.exit()

if x==y==999999:
    print(151840682)
    sys.exit()

from functools import lru_cache
@lru_cache(maxsize=1000000)
def c(n,k): 
  return 1 if(k<=0 or n<=k) else (c(n-1, k-1) + c(n-1, k))%D

sys.setrecursionlimit(1000000)
print(c(T//3,W))


### FIXED CODE ###

import sys

x,y=map(int,input().split())
T=x+y
MOD=10**9+7

def nCmMOD(A,B,Mod):
    num,den=1,1
    for i in range(B):
        num*=(A-i)
        den*=(i+1)
        num%=Mod
        den%=Mod
    return (num*pow(den,Mod-2,Mod))%Mod

if T%3!=0 or y/x<1/2 or y/x>2:
    print(0)
    sys.exit()
else:
    n=(2*y-x)//3
    m=(2*x-y)//3
    l=min(n,m)
    print(nCmMOD(n+m,l,MOD))
import heapq, bisect
n = int(input())
s = list(input())
alp = 'abcdefghijklmnopqrstuvwxyz'
cnt = {c: [] for c in alp}

for i in range(n):
    heapq.heappush(cnt[s[i]], i)

q = int(input())
for _ in range(q):
    t, x, y = input().split()
    ans = 0
    if t == '1':
        i, c = int(x) - 1, y
        cnt[s[i]].remove(i)
        heapq.heappush(cnt[c], i)
    else:
        l, r = int(x) - 1, int(y) - 1
        for c in alp:
            a = bisect.bisect_left(cnt[c], l)
            if a < len(cnt[c]) and cnt[c][a] <= r:
                ans += 1
        print(ans)

### FIXED CODE ###

import bisect
n = int(input())
s = list(input())
alp = 'abcdefghijklmnopqrstuvwxyz'
cnt = {c: [] for c in alp}

for i, c in enumerate(s):
    cnt[c].append(i)

q = int(input())
for _ in range(q):
    t, x, y = input().split()
    ans = 0
    if t == '1':
        i, c = int(x) - 1, y
        if s[i] == c:
            continue
        a = bisect.bisect_left(cnt[s[i]], i)
        cnt[s[i]].pop(a)
        s[i] = c
        b = bisect.bisect_left(cnt[c], i)
        cnt[c].insert(b, i)
    else:
        l, r = int(x) - 1, int(y) - 1
        for c in alp:
            a = bisect.bisect_left(cnt[c], l)
            if a < len(cnt[c]) and cnt[c][a] <= r:
                ans += 1

        print(ans)
import sys
input = sys.stdin.readline
N,K = map(int,input().split())
V = list(map(int,input().split()))

D = []
ans = 0

for i in range(K+1):
    for j in range(K-i+1):
        if i + j > N:
            break
        D.append(V[:i]+V[N-j:])

for d in D:
    for x in range(K-len(d)):
        if d==[]:
            break
        elif min(d)<0:
            d.pop(d.index(min(d)))

        elif min(d)>0:
            break
        
        if d!=[]:
            s = sum(d)
            if s>0 and s>ans:
                ans = s

print(s)

### FIXED CODE ###

import sys
input = sys.stdin.readline
N,K = map(int,input().split())
V = list(map(int,input().split()))

D = []
ans = []

for i in range(K+1):
    for j in range(K-i+1):
        if i + j > N:
            break
        D.append(V[:i]+V[N-j:])

for d in D:
    for x in range(K-len(d)):
        if d==[]:
            break
        elif min(d)<0:
            d.pop(d.index(min(d)))

        elif min(d)>0:
            break
        
    if d==[]:
        ans.append(0)
    elif sum(d)>0:
        ans.append(sum(d))

if max(ans)>0:
    print(max(ans))

else:
    print(0)
n=int(input())
d,x=map(int,input().split())
s=0
for i in range(n):
    w=0
    while d>=w*int(input())+1:
        s+=1
        w+=1
print(s+x)

### FIXED CODE ###

n=int(input())
d,x=map(int,input().split())
s=0
for i in range(n):
    w=0
    a=int(input())
    while d>=w*a+1:
        s+=1
        w+=1
print(s+x)
# At Beginner Contest 042 B (200)
n, l = map(int, input().split())
S = []
for _ in range(n):
    S.append(input())

out = []
min_ = float("inf")
min_idxs = list(range(len(S)))
pos = 0
while S:
    new_min = []
    for i in min_idxs:
        s = S[i]
        ch = ord(s[pos])
        if ch < min_:
            min_ = ord(s[pos])
            new_min = [ i ]
        elif ch == min_:
            new_min.append(i)
    if len(new_min) == 1:
        out.append(S[new_min[0]])
        del S[new_min[0]]
        min_idxs = list(range(len(S)))
        pos = 0
    else:
        pos += 1
        min_idxs = new_min
    min_ = float("inf")

r = "".join(out)
print(r)


### FIXED CODE ###

# At Beginner Contest 042 B (200)
n, l = map(int, input().split())
S = []
for _ in range(n):
    S.append(input())

out = []
min_ = float("inf")
min_idxs = list(range(len(S)))
pos = 0
while S:
    new_min = []
    for i in min_idxs:
        s = S[i]
        ch = ord(s[pos])
        if ch < min_:
            min_ = ord(s[pos])
            new_min = [ i ]
        elif ch == min_:
            new_min.append(i)
    if len(new_min) == 1 or pos == l - 1:
        for i in range(len(new_min)):
            out.append(S[new_min[i]])
        for i in range(len(new_min) - 1, -1, -1):
            del S[new_min[i]]
        min_idxs = list(range(len(S)))
        pos = 0
    else:
        pos += 1
        min_idxs = new_min
    min_ = float("inf")

r = "".join(out)
print(r)
n=int(input())
a=list(map(int,input().split()))
i=0
con=1
mod=10**18
ans=1
why=0
while con==1:
  ans=ans*a[i]
  i+=1
  if ans>mod:
    ans=1
    why=1
  elif i==n:
    con=0
if ans==0:
  print(0)
elif why==1:
  print(-1)
else:
  print(ans)


### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
i=0
con=1
mod=10**18
ans=1
why=0
while con==1:
  ans=ans*a[i]
  i+=1
  if ans>mod and i!=n:
    ans=1
    why=1
  elif ans>mod and i==n:
    con=0
    ans=1
    why=1
  elif i==n:
    con=0
if ans==0:
  print(0)
elif why==1:
  print(-1)
else:
  print(ans)
n = int(input())
num_list = [input() for i in range(n)]

bucket = {}
for i in range(100):
    bucket[i] = 0
for i in range(n):
    bucket[int(num_list[i])] += 1
res = 0
for i in range(100):
    if bucket[i] > 0:
        res += 1

print(res)


### FIXED CODE ###

n = int(input())
d_list = [input() for i in range(n)]

unique_num_dict = {}
for i in range(n):
    if d_list[i] in unique_num_dict.keys():
        unique_num_dict[d_list[i]] += 1
    else:
        unique_num_dict[d_list[i]] = 1

print(len(unique_num_dict))
n = int(input())
h = list(map(int, input().split()))
hantei = [0 for _ in range(n)]
hantei.append(1)
result = 0
h.append(10*3)

while any(hantei[i] != 1 for i in range(n)):
    min_h = min(h)
    cost = 0
    for i in range(len(h)):
        if hantei[i]==1:
            result += cost
            cost = 0
            h[i] = 10**3
        else:
            h[i] -= min_h
            if h[i] == 0:
                hantei[i] = 1
            cost = min_
print(result)

### FIXED CODE ###

n = int(input())
h = list(map(int, input().split()))
hantei = [0 for _ in range(n)]
hantei.append(1)
result = 0
h.append(10*3)

while any(hantei[i] != 1 for i in range(n)):
    min_h = min(h)
    cost = 0
    for i in range(len(h)):
        if hantei[i]==1:
            result += cost
            cost = 0
            h[i] = 10**3
        else:
            h[i] -= min_h
            if h[i] == 0:
                hantei[i] = 1
            cost = min_h
print(result)
mozi = str(input())
s = str(x)
for i in range(len(mozi)):
  s += "x"
print(s)

### FIXED CODE ###

mozi = str(input())
s = str()
for i in range(len(mozi)):
  s += str("x")
print(s)
import numpy as np

H, W, M = map(int, input().split())
row = np.zeros(W)
col = np.zeros(H)
memo = []
for _ in range(M):
    h, w = map(int, input().split())
    h -=1
    w -=1
    row[w] += 1
    col[h] += 1
    memo.append((h, w))

col_max = col.max()
row_max = row.max()

max_col_indexs = np.where(col == col_max)[0]
max_row_indexs = np.where(row == row_max)[0]

ans = col_max + row_max -1
for c in max_col_indexs:
    for r in max_row_indexs:
        if (c, r) not in memo:
            ans = ans = col_max + row_max
            break

print(int(ans))

### FIXED CODE ###

import numpy as np
def main():
    H, W, M = map(int, input().split())
    row = np.zeros(W)
    col = np.zeros(H)
    memo = []
    for _ in range(M):
        h, w = map(int, input().split())
        h -=1
        w -=1
        row[w] += 1
        col[h] += 1
        memo.append((h, w))

    col_max = col.max()
    row_max = row.max()

    max_col_indexs = np.where(col == col_max)[0]
    max_row_indexs = np.where(row == row_max)[0]

    ans = col_max + row_max -1
    memo = set(memo)
    for c in max_col_indexs:
        for r in max_row_indexs:
            if (c, r) not in memo:
                ans = ans = col_max + row_max
                print(int(ans))
                exit()

    print(int(ans))

if __name__ == "__main__":
    main()
import itertools

N, M, Q = map(int, input().split())

Q_list = []
for _ in range(Q):
    q = list(map(int, input().split()))
    Q_list.append(q)

A_list = []
for a in itertools.combinations_with_replacement(range(1, M+1), N):
    if a[0]<= a[1] and a[1] <= a[2]:
        A_list.append(a)

score_list = []
for A in A_list:
    score = 0
    for i in range(Q):
        a, b, c, d = Q_list[i][0], Q_list[i][1], Q_list[i][2], Q_list[i][3]
        a -=1
        b -=1
        if A[b] - A[a] == c:
            score += d
    score_list.append(score)

print(max(score_list))

### FIXED CODE ###

import itertools

N, M, Q = map(int, input().split())

Q_list = []
for _ in range(Q):
    a, b, c, d = list(map(int, input().split()))
    Q_list.append((a, b, c, d))

A_list = []
for a in itertools.combinations_with_replacement(range(1, M+1), N):
    A_list.append(a)

answer = 0
score_list = []
for A in A_list:
    score = 0
    for i in range(Q):
        a, b, c, d = Q_list[i]
        a -=1
        b -=1
        if A[b] - A[a] == c:
            score += d
    answer = max(answer, score)

print(answer)
#!/usr/bin/env python3
import sys


class Node:
    def __init__(self, index):
        self.index = index
        self.ref = []
        self.visited = False

    def append(self, node):
        self.ref.append(node)
        node.ref.append(self)

    def visit(self):
        self.visited = True
        for r in self.ref:
            if not r.visited:
                r.visit()


def solve(N: int, M: int, X: "List[int]", Y: "List[int]", Z: "List[int]"):
    nodes = [Node(i) for i in range(N)]
    X = [x-1 for x in X]
    Y = [y-1 for y in Y]
    for i in range(M):
        nodes[X[i]].append(nodes[Y[i]])
    sum = 0
    for i in range(N):
        if not nodes[i].visited:
            sum += 1
            nodes[i].visit()
    print(sum)
    return


# Generated by 1.1.6 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()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    X = [int()] * (M)  # type: "List[int]"
    Y = [int()] * (M)  # type: "List[int]"
    Z = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        X[i] = int(next(tokens))
        Y[i] = int(next(tokens))
        Z[i] = int(next(tokens))
    solve(N, M, X, Y, Z)


if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)


class Node:
    def __init__(self, index):
        self.index = index
        self.ref = []
        self.visited = False

    def append(self, node):
        self.ref.append(node)
        node.ref.append(self)

    def visit(self):
        self.visited = True
        for r in self.ref:
            if not r.visited:
                r.visit()


def solve(N: int, M: int, X: "List[int]", Y: "List[int]", Z: "List[int]"):
    nodes = [Node(i) for i in range(N)]
    X = [x-1 for x in X]
    Y = [y-1 for y in Y]
    for i in range(M):
        nodes[X[i]].append(nodes[Y[i]])
    sum = 0
    for i in range(N):
        if not nodes[i].visited:
            sum += 1
            nodes[i].visit()
    print(sum)
    return


# Generated by 1.1.6 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()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    X = [int()] * (M)  # type: "List[int]"
    Y = [int()] * (M)  # type: "List[int]"
    Z = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        X[i] = int(next(tokens))
        Y[i] = int(next(tokens))
        Z[i] = int(next(tokens))
    solve(N, M, X, Y, Z)


if __name__ == '__main__':
    main()