//SRC be locked
var i,j,k,l,m,n,p,q,r,s,t:longint;
root,len:array[1..500] of longint;
//SRC be locked
function f(x:longint):longint;
var s:longint;
begin
if root[x]=x then exit(x);
s:=root[x];
root[x]:=f(root[x]);
len[x]:=len[x]+len[s];
exit(root[x]);
end;
//SRC be locked
begin
read(n,p);
for i:=1 to n do root[i]:=i;
for i:=1 to p do
begin
read(l,r,t);
if f(l)=f(r) then
begin
if len[r]-len[l]<>t then
begin
writeln(-1);
halt;
end;
end else
begin
len[root[r]]:=t+len[l]-len[r];
root[root[r]]:=root[l];
end;
end;
for i:=1 to n do root[i]:=f(i);
k:=0;
for i:=1 to n do if len[i]<k then k:=len[i];
for i:=1 to n do len[i]:=len[i]-k;
for i:=1 to n do writeln(len[i]);
end.
//SRC be locked
var i,j,k,l,m,n,p,q,r,s,t:longint;
root,len:array[1..500] of longint;
//SRC be locked
function f(x:longint):longint;
var s:longint;
begin
if root[x]=x then exit(x);
s:=root[x];
root[x]:=f(root[x]);
len[x]:=len[x]+len[s];
exit(root[x]);
end;
//SRC be locked
begin
read(n,p);
for i:=1 to n do root[i]:=i;
for i:=1 to p do
begin
read(l,r,t);
if f(l)=f(r) then
begin
if len[r]-len[l]<>t then
begin
writeln(-1);
halt;
end;
end else
begin
len[root[r]]:=t+len[l]-len[r];
root[root[r]]:=root[l];
end;
end;
for i:=1 to n do root[i]:=f(i);
k:=0;
for i:=1 to n do if len[i]<k then k:=len[i];
for i:=1 to n do len[i]:=len[i]-k;
for i:=1 to n do writeln(len[i]);
end.
//SRC be locked