You are given an undirected weighted connected graph with $n$ vertices and $m$ edges without loops and multiple edges.

The $i$-th edge is $e_i = (u_i, v_i, w_i)$; the distance between vertices $u_i$ and $v_i$ along the edge $e_i$ is $w_i$ ($1 \le w_i$). The graph is connected, i. e. for any pair of vertices, there is at least one path between them consisting only of edges of the given graph.

A minimum spanning tree (MST) in case of positive weights is a subset of the edges of a connected weighted undirected graph that connects all the vertices together and has minimum total cost among all such subsets (total cost is the sum of costs of chosen edges).

You can modify the given graph. The only operation you can perform is the following: increase the weight of some edge by $1$. You can increase the weight of each edge multiple (possibly, zero) times.

Suppose that the initial MST cost is $k$. Your problem is to increase weights of some edges with minimum possible number of operations in such a way that the cost of MST in the obtained graph remains $k$, but MST is unique (it means that there is only one way to choose MST in the obtained graph).

Your problem is to calculate the minimum number of operations required to do it.


-----Input-----

The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5, n - 1 \le m \le 2 \cdot 10^5$) — the number of vertices and the number of edges in the initial graph.

The next $m$ lines contain three integers each. The $i$-th line contains the description of the $i$-th edge $e_i$. It is denoted by three integers $u_i, v_i$ and $w_i$ ($1 \le u_i, v_i \le n, u_i \ne v_i, 1 \le w \le 10^9$), where $u_i$ and $v_i$ are vertices connected by the $i$-th edge and $w_i$ is the weight of this edge.

It is guaranteed that the given graph doesn't contain loops and multiple edges (i.e. for each $i$ from $1$ to $m$ $u_i \ne v_i$ and for each unordered pair of vertices $(u, v)$ there is at most one edge connecting this pair of vertices). It is also guaranteed that the given graph is connected.


-----Output-----

Print one integer — the minimum number of operations to unify MST of the initial graph without changing the cost of MST.


-----Examples-----
Input
8 10
1 2 1
2 3 2
2 4 5
1 4 2
6 3 3
6 1 3
3 5 2
3 7 1
4 8 1
6 2 4

Output
1

Input
4 3
2 1 3
4 3 4
2 4 1

Output
0

Input
3 3
1 2 1
2 3 2
1 3 3

Output
0

Input
3 3
1 2 1
2 3 3
1 3 3

Output
1

Input
1 0

Output
0

Input
5 6
1 2 2
2 3 1
4 5 3
2 4 2
1 4 2
1 5 3

Output
2



-----Note-----

The picture corresponding to the first example: [Image]

You can, for example, increase weight of the edge $(1, 6)$ or $(6, 3)$ by $1$ to unify MST.

The picture corresponding to the last example: $\$ 8$

You can, for example, increase weights of edges $(1, 5)$ and $(2, 4)$ by $1$ to unify MST.

Difficulty: introductory

Starter code:

Solutions:
import sys
input = sys.stdin.readline

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

EDGE.sort(key=lambda x:x[2])

WCHANGE=[]
for i in range(1,m):
    if EDGE[i-1][2]!=EDGE[i][2]:
        WCHANGE.append(i)

WCHANGE.append(m)

Group=[i for i in range(n+1)]

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        Group[find(y)]=Group[find(x)]=min(find(y),find(x))


NOW=0
noneed=[0]*m
ANS=0
for wc in WCHANGE:
    for j in range(NOW,wc):

        if find(EDGE[j][0])==find(EDGE[j][1]):
            noneed[j]=1

    for j in range(NOW,wc):
        if noneed[j]==1:
            continue      
        x,y,w=EDGE[j]
        if find(x)!=find(y):
            Union(x,y)
        else:
            ANS+=1

    NOW=wc

print(ANS)
        


import sys
input = sys.stdin.readline

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

EDGE.sort(key=lambda x:x[2])

WCHANGE=[]
for i in range(1,m):
    if EDGE[i-1][2]!=EDGE[i][2]:
        WCHANGE.append(i)

WCHANGE.append(m)

Group=[i for i in range(n+1)]

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        Group[find(y)]=Group[find(x)]=min(find(y),find(x))


NOW=0
noneed=[0]*m
ANS=0
for wc in WCHANGE:
    for j in range(NOW,wc):

        if find(EDGE[j][0])==find(EDGE[j][1]):
            noneed[j]=1

    for j in range(NOW,wc):
        if noneed[j]==1:
            continue      
        x,y,w=EDGE[j]
        if find(x)!=find(y):
            Union(x,y)
        else:
            ANS+=1

    NOW=wc

print(ANS)
        


n, m = list(map(int, input().split()))
e = [tuple(map(int, input().split())) for i in range(m)]
e = sorted((w, u, v) for u, v, w in e)

p, r = [i for i in range(n + 1)], [0] * (n + 1)

def find(x):
    if p[x] != x:
        p[x] = find(p[x])
    return p[x]

def union(x, y):
    x, y = p[x], p[y]
    if r[x] < r[y]:
        p[x] = y
    else:
        p[y] = x
        if r[x] == r[y]:
            r[x] += 1

ans = 0
i = 0
while i < m:
    j = i + 1
    while j < m and e[j][0] == e[i][0]:
        j += 1
    tmp = [(u, v) for w, u, v in e[i:j] if find(u) != find(v)]
    for u, v in tmp:
        if find(u) == find(v):
            ans += 1
        else:
            union(u, v)
    i = j

print(ans)


# -*- coding: utf-8 -*-
# @Time    : 2019/1/23 23:46
# @Author  : LunaFire
# @Email   : gilgemesh2012@gmail.com
# @File    : F. MST Unification.py


class UnionFind(object):

    def __init__(self, n):
        self.root = [i for i in range(n)]
        self.size = [1 for i in range(n)]

    def find(self, x):
        if self.root[x] == x:
            return x
        self.root[x] = self.find(self.root[x])
        return self.root[x]

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

        if self.size[x] < self.size[y]:
            x, y = y, x
        self.size[x] += self.size[y]
        self.root[y] = x
        return True


def main():
    n, m = list(map(int, input().split()))
    edge_list = []
    for _ in range(m):
        edge_list.append(list(map(int, input().split())))

    union_find = UnionFind(n + 1)
    edge_list.sort(key=lambda x: x[2])

    p1 = ret = 0
    while p1 < m:
        p2 = p1
        while p2 < m and edge_list[p1][2] == edge_list[p2][2]:
            p2 += 1

        avail = used = 0
        for i in range(p1, p2):
            if union_find.find(edge_list[i][0]) != union_find.find(edge_list[i][1]):
                avail += 1
        for i in range(p1, p2):
            if union_find.union(edge_list[i][0], edge_list[i][1]):
                used += 1
        ret += avail - used
        p1 = p2
    print(ret)


def __starting_point():
    main()

__starting_point()

n, m = map(int, input().split())
e = [tuple(map(int, input().split())) for i in range(m)]
e = sorted((w, u, v) for u, v, w in e)

p, r = [i for i in range(n + 1)], [0] * (n + 1)

def find(x):
    if p[x] != x:
        p[x] = find(p[x])
    return p[x]

def union(x, y):
    x, y = p[x], p[y]
    if r[x] < r[y]:
        p[x] = y
    else:
        p[y] = x
        if r[x] == r[y]:
            r[x] += 1

ans = 0
i = 0
while i < m:
    j = i + 1
    while j < m and e[j][0] == e[i][0]:
        j += 1
    tmp = [(u, v) for w, u, v in e[i:j] if find(u) != find(v)]
    for u, v in tmp:
        if find(u) == find(v):
            ans += 1
        else:
            union(u, v)
    i = j

print(ans)

class DisjoinSet:
    def __init__(self, n):
        self.n = n
        self.dad = [i for i in range(n + 1)]
        self.cant = [1] * (n + 1)

    def SetOf(self, n):
        if self.dad[n] == n:
            return n
        self.dad[n] = self.SetOf(self.dad[n])
        return self.dad[n]

    def Merge(self, a, b):
        a, b = self.SetOf(a), self.SetOf(b)
        if a != b:
            if self.cant[b] > self.cant[a]:
                a, b = b, a
            self.cant[a] += self.cant[b]
            self.dad[b] = a
            return True
        return False


def Kruskal(l, n):
    l.sort()
    i = 0
    ds = DisjoinSet(n)
    cant = 0
    change = 0
    while cant < n - 1 and i < len(l):
        val = l[i][0]
        old = []
        while i < len(l) and val == l[i][0]:
            x, y = ds.SetOf(l[i][1]), ds.SetOf(l[i][2])
            if x != y:
                old.append((x, y))
            i += 1
        change += len(old)
        for (x, y) in old:
            change -= ds.Merge(x, y)
    return change


n, m = input().split()
n, m = int(n), int(m)

e = []
for i in range(m):
    x, y, c = input().split()
    e.append((int(c), int(x), int(y)))

print(Kruskal(e, n))


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

l = []
for i in range(m):
	a,b,c = map(int,input().split())
	l.append((c,a,b))
l.sort()
f = [i for i in range(n+1)]
def r(a):
    if a==f[a]:
        return a
    f[a]=r(f[a])
    return f[a]

cnt = 0
i=0
while i < m:
    j = i
    while j < m and l[j][0] == l[i][0]:
        j=j+1
    num=0
    for k in range(i,j):
        x=r(l[k][1])
        y=r(l[k][2])
        if x!=y:
            num=num+1
    for k in range(i,j):
        x = r(l[k][1])
        y = r(l[k][2])
        if x != y:
            f[y]=x
            num = num -1
    cnt=cnt+num
    i=j
print(cnt)

a=input().split()
n,m=int(a[0]),int(a[1])
f=[i for i in range(0,n+1)]
class edge:
    def __init__(self,U,V,W):
        self.u=U
        self.v=V
        self.w=W

l=[]
for i in range(m):
    a = input().split()
    u,v,w=int(a[0]),int(a[1]),int(a[2])
    l.append(edge(u,v,w))
l.sort(key=lambda x:x.w)

def r(a):
    if a!=f[a]:
        f[a]=r(f[a])
    return f[a]
cnt=0
i=0
while i<m:
    j=i
    while l[i].w==l[j].w:
        j=j+1
        if j==m:
            break
    num=0
    for k in range(i,j):
        x=r(l[k].u)
        y=r(l[k].v)
        if x!=y:
            num=num+1
    for k in range(i, j):
        x = r(l[k].u)
        y = r(l[k].v)
        if x != y:
            f[y]=x
            num = num -1
    cnt=cnt+num
    i=j
print(cnt)

n,m=map(int,input().split())
class edge:
    def __init__(self,U,V,W):
        self.u=U
        self.v=V
        self.w=W

l=[]
for i in range(m):
    u,v,w=map(int,input().split())
    l.append(edge(u,v,w))
l.sort(key=lambda x:x.w)
f=[i for i in range(0,n+1)]
def r(a):
    if a!=f[a]:
        f[a]=r(f[a])
    return f[a]
cnt=0
i=0
while i<m:
    j=i
    while l[i].w==l[j].w:
        j=j+1
        if j==m:
            break
    num=0
    for k in range(i,j):
        x=r(l[k].u)
        y=r(l[k].v)
        if x!=y:
            num=num+1
    for k in range(i, j):
        x = r(l[k].u)
        y = r(l[k].v)
        if x != y:
            f[y]=x
            num = num -1
    cnt=cnt+num
    i=j
print(cnt)

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

l = []
for i in range(m):
	a,b,c = map(int,input().split())
	l.append((c,a,b))
l.sort()
f = [i for i in range(n+1)]
def r(a):
    if a!=f[a]:
        f[a]=r(f[a])
    return f[a]

cnt = 0
i=0
while i < m:
    j = i
    while j < m and l[j][0] == l[i][0]:
        j=j+1
    num=0
    for k in range(i,j):
        x=r(l[k][1])
        y=r(l[k][2])
        if x!=y:
            num=num+1
    for k in range(i,j):
        x = r(l[k][1])
        y = r(l[k][2])
        if x != y:
            f[y]=x
            num = num -1
    cnt=cnt+num
    i=j
print(cnt)

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

l = []
for i in range(m):
	a,b,c = map(int,input().split())
	l.append((c,a,b))
l.sort()
f = [i for i in range(n+1)]
c=[1]*(n+1)
def r(a):
    if a!=f[a]:
        f[a]=r(f[a])
    return f[a]
def union(x,y):
    if x==y:
        return
    if c[x]<c[y]:
        f[x]=y
    else:
        if c[x]==c[y]:
            c[x]=c[x]+1
        f[y]=x

cnt = 0
i=0
while i < m:
    j = i
    while j < m and l[j][0] == l[i][0]:
        j=j+1
    num=0
    for k in range(i,j):
        x=r(l[k][1])
        y=r(l[k][2])
        if x!=y:
            num=num+1
    for k in range(i,j):
        x = r(l[k][1])
        y = r(l[k][2])
        if x != y:
            union(x,y)
            num = num -1
    cnt=cnt+num
    i=j
print(cnt)

# Anton Timofeev
import sys


def get(v):
	if v == prev[v]:
		return v
	prev[v] = get(prev[v])
	return prev[v]

def join(u, v):
	u = get(u); v = get(v)
	if u != v:
		if size[u] < size[v]:
			u, v = v, u
		prev[v] = u
		size[u] += size[v]


n, m = map(int, sys.stdin.readline().split())
edges = [0] * m
for i in range(m):
	u, v, w = map(int, sys.stdin.readline().split())
	edges[i] = (u - 1, v - 1, w)

edges.sort(key=lambda e: e[2])
# print(edges)

prev = list(range(n))
size = [1] * n

res = 0
l, r = 0, 1
while l < m:
	while r < m and edges[l][2] == edges[r][2]:
		r += 1
	res += r - l
	for i in range(l, r):
		if get(edges[i][0]) == get(edges[i][1]):
			res -= 1
	for i in range(l, r):
		if get(edges[i][0]) != get(edges[i][1]):
			join(edges[i][0], edges[i][1])
			# print(f"join: {edges[i]}")
			res -= 1
	l = r
print(res)

import sys
# 26
input = lambda: sys.stdin.readline().strip()
ipnut = input
def find(a):
    st = []
    while a!=dsu[a]:
        st.append(a)
        a = dsu[a]
    for j in st:
        dsu[j]=a
    return a
def union(a,b,f):
    a = find(a)
    b = find(b)
    if f:
        dsu[a]=b
    else:
        dsu[b]=a
n,m = map(int,input().split())
if m == 0:
    print(0)
    return
dsu = [i for i in range(n)]
h = []
for i in range(m):
    a,b,c = map(int,ipnut().split())
    h.append((c,a-1,b-1))
h.sort()
lc = 0
ans = 0
heh = [[h[0]]]
for i in h[1:]:
    if heh[-1][0][0]==i[0]:
        heh[-1].append(i)
    else:
        heh.append([i])
ab = []
for h in heh:
    lol = []
    for c,a,b in h:
        if find(a)!=find(b):
            lol.append((a,b))
    for a,b in lol:
        if find(a)==find(b):
            ans+=1
        else:
            union(a,b,ans%2)
print(ans)

class DisjoinSet:
    def __init__(self, n):
        self.n = n
        self.dad = [i for i in range(n + 1)]
        self.cant = [1] * (n + 1)

    def SetOf(self, n):
        if self.dad[n] == n:
            return n
        self.dad[n] = self.SetOf(self.dad[n])
        return self.dad[n]

    def Merge(self, a, b):
        a, b = self.SetOf(a), self.SetOf(b)
        if a != b:
            if self.cant[b] > self.cant[a]:
                a, b = b, a
            self.cant[a] += self.cant[b]
            self.dad[b] = a
            return True
        return False


def Kruskal(l, n):
    l.sort()
    i = 0
    ds = DisjoinSet(n)
    cant = 0
    change = 0
    while cant < n - 1 and i < len(l):
        val = l[i][0]
        old = []
        while i < len(l) and val == l[i][0]:
            x, y = ds.SetOf(l[i][1]), ds.SetOf(l[i][2])
            if x != y:
                old.append((x, y))
            i += 1
        change += len(old)
        for (x, y) in old:
            change -= ds.Merge(x, y)
    return change


n, m = input().split()
n, m = int(n), int(m)

e = []
for i in range(m):
    x, y, c = input().split()
    e.append((int(c), int(x), int(y)))

print(Kruskal(e, n))



n,m = list(map(int,input().split()))
     
l = []
for i in range(m):
    a,b,c = list(map(int,input().split()))
    l.append((c,a,b))
l.sort()
f = [i for i in range(n+1)]
def r(a):
    if a!=f[a]:
        f[a]=r(f[a])
    return f[a]
     
cnt = 0
i=0
while i < m:
    j = i
    while j < m and l[j][0] == l[i][0]:
        j=j+1
    num=0
    for k in range(i,j):
        x=r(l[k][1])
        y=r(l[k][2])
        if x!=y:
            num=num+1
    for k in range(i,j):
        x = r(l[k][1])
        y = r(l[k][2])
        if x != y:
            f[y]=x
            num = num -1
    cnt=cnt+num
    i=j
print(cnt)
#JSR


n,m = list(map(int,input().split()))
     
l = []
for i in range(m):
    a,b,c = list(map(int,input().split()))
    l.append((c,a,b))
l.sort()
f = [i for i in range(n+1)]
def r(a):
    if a!=f[a]:
        f[a]=r(f[a])
    return f[a]
     
cnt = 0
i=0
while i < m:
    j = i
    while j < m and l[j][0] == l[i][0]:
        j=j+1
    num=0
    for k in range(i,j):
        x=r(l[k][1])
        y=r(l[k][2])
        if x!=y:
            num=num+1
    for k in range(i,j):
        x = r(l[k][1])
        y = r(l[k][2])
        if x != y:
            f[y]=x
            num = num -1
    cnt=cnt+num
    i=j
print(cnt)


# https://codeforces.com/problemset/problem/1108/F
n, m =  map(int, input().split())
edge =  [list(map(int, input().split())) for _ in range(m)]
edge =  sorted(edge,key=lambda x: x[2])

class Union:
    def __init__(self, n):
        self.p    = [i for i in range(n+1)]
        self.rank = [0] * (n+1) 
        
    def find(self, x):
        if self.p[x] != x:
            self.p[x] = self.find(self.p[x])
        return self.p[x]
    
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        
        if x != y:
            if self.rank[x] < self.rank[y]:
                self.p[x]     = y
                self.rank[y] += self.rank[x]
            else:
                self.p[y]     = x
                self.rank[x] += self.rank[y]
            
cnt  =  0
used = [0] * m
l, r = 0, 1
U    = Union(n)

while l < m:
    try:
        while r < m and edge[r][2] == edge[l][2]:
            r+=1
    except:
        print('bl1')
    
    try:
        for i in range(l, r):
            u, v, w = edge[i] 
            if U.find(u) == U.find(v):
                used[i] = 1
    except:
        print('bl2')
        
    try:    
        for i in range(l, r):
            if used[i] == 1:
                continue
            
            u, v, w = edge[i]    
            if U.find(u) == U.find(v):
                cnt +=1
            else:
                U.union(u, v)  
    except:
        print('bl3')
        print(u, v)
        print(U.find(u))
        print(U.find(v))
        
    l = r
    r = l+1
    
print(cnt)    

import collections as cc
import itertools as it
import bisect as bi
I=lambda:list(map(int,input().split()))
n,m=I()
ed=[I() for i in range(m)]
ed.sort(key=lambda x:x[2])
ch=[]
for i in range(1,m):
	if ed[i-1][2]!=ed[i][2]:
		ch.append(i)
ch.append(m)
parent=[i for i in range(n+1)]
def find(x):
	while x!=parent[x]:
		x=parent[x]
	return x
def union(x,y):
	a=find(x)
	b=find(y)
	parent[a]=parent[b]=min(a,b)
cur=0
 
cur=0
no=[0]*m
ans=0
for i in ch:
    for j in range(cur,i):
        if find(ed[j][0])==find(ed[j][1]):
            no[j]=1
    for j in range(cur,i):
        if no[j]==1:
            continue      
        x,y,w=ed[j]
        if find(x)!=find(y):
            union(x,y)
        else:
            ans+=1
    cur=i
 

print(ans)

import sys
input = sys.stdin.readline
I = lambda : list(map(int,input().split()))

n,m=I()
ed=[I() for i in range(m)]
ed.sort(key=lambda x:x[2])
p=[i for i in range(n)]

def find(x):
	while x!=p[x]:
		x=p[x]
	return x

def union(a,b):
	x=find(a)
	y=find(b)
	if x!=y:
		p[y]=p[x]=min(x,y)
		return 1
	return 0
an=0;ce=0
pos=[1]*m
i=0
while ce<n-1:
	#print(i,ce,pos)
	x,y,w=ed[i]
	for j in range(i,m):
		if ed[j][2]!=w:
			break
		a,b,c=ed[j]
		if find(a-1)==find(b-1):
			pos[j]=0
	for j in range(i,m):
		x,y,b=ed[j]
		if ed[j][2]!=w:
			break
		if pos[j]:
			d=union(x-1,y-1)
			#print(d,p)
			if d==0:
				an+=1
			else:
				ce+=1
	if ce==n-1:
		break
	i=j

print(an)

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

l = []
for i in range(m):
	a,b,c = map(int,input().split())
	l.append((c,a,b))
l.sort()
f = [i for i in range(n+1)]
def r(a):
    if a==f[a]:
        return a
    f[a]=r(f[a])
    return f[a]

cnt = 0
i=0
while i < m:
    j = i
    while j < m and l[j][0] == l[i][0]:
        j=j+1
    num=0
    for k in range(i,j):
        x=r(l[k][1])
        y=r(l[k][2])
        if x!=y:
            num=num+1
    for k in range(i,j):
        x = r(l[k][1])
        y = r(l[k][2])
        if x != y:
            f[y]=x
            num = num -1
    cnt=cnt+num
    i=j
print(cnt)