import sys
import heapq
input= sys.stdin.readline
n,m= map(int, input().split())
gra=[[]for _ in range(n+1)]
visit= [0 for _ in range(n+1)]
total = 0
ans =0
for i in range(m):
a,b,c = map(int, input().split())
total += c
gra[a].append((b,c))
gra[b].append((a,c))
def prim(start):
global ans
q = [[0,start]]
heapq.heapify(q)
while q :
cost, now =heapq.heappop(q)
if visit[now] ==0:
visit[now] = 1
ans+= cost
for nextnode, nextcost in gra[now]:
if visit[nextnode] ==0 :
heapq.heappush(q,(nextcost,nextnode))
prim(1)
print(total-ans if (0 not in visit[1:]) else -1)