from collections import deque

import sys

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

table=[[] for i in range(N)]

ans=[]

for i in range(M):

    a,b,c=list(map(int,input().split()))

    table[a-1].append((b-1,c))

    table[b-1].append((a-1,c))

    ans.append((a-1,b-1,c))

h=deque()

inf=-10**13

h.append(0)

flag=True

visit=[(inf,0)]*N

visit[0]=(0,0)

tree=[]

while h:

    x=h.popleft()

    for y,d in table[x]:

        if visit[y][0]!=inf and visit[y][1]==visit[x][1]:

            flag =False

            tree.append((x,y,d))

        elif visit[y][0]!=inf:

            if (visit[x][0]+visit[y][0])!=d:

                print((0))

                sys.exit()

        if visit[y][0]==inf:

            t=(visit[x][1]+1)%2

            visit[y]=(d-visit[x][0],t)

            h.append(y)

if flag:

    mi=-inf

    ma=-inf

    for i in range(N):

        if visit[i][1]==0:

            mi=min(mi,visit[i][0])

        else:

            ma=min(ma,visit[i][0])

    print((max(0,ma+mi-1)))

    sys.exit()

a,b,c=tree[0]

s=visit[a][1]

t=visit[a][0]+visit[b][0]

w=(c-t)//2

for i in range(N):

    if visit[i][1]==s:

        x=visit[i][0]

        visit[i]=(x+w,s)

    else:

        x=visit[i][0]

        visit[i]=(x-w,(s+1)%2)

for x,y,d in ans:

    if (visit[x][0]+visit[y][0])!=d:

        print((0))

        sys.exit()

for i in range(N):

    if visit[i][0]<=0:

        print((0))

        sys.exit()

print((1))
import sys

from collections import deque

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

table=[[] for i in range(N)]

for i in range(M):

    u,v,s=list(map(int,input().split()))

    table[u-1].append((v-1,s))

    table[v-1].append((u-1,s))

L=[0]*N

T=[1]*N#checker

visit=[False]*N

h=deque()

h.append([0,1])

visit[0]=True

flag=True



K=[]



while h:

    x,t=h.popleft()

    for y,s in table[x]:

        if visit[y]:

            if T[y]==T[x]:

                flag=False

                K.append((x,y,s))

            else:

                if L[y]+L[x]!=s:

                    print((0))

                    sys.exit()

            continue

        T[y]=-t

        visit[y]=True

        L[y]=s-L[x]

        h.append([y,-t])

    

    

if not flag:

    x,y,s=K[0]

    t=T[x]

    u=L[x]+L[y]-s

    if u%2!=0:

        print((0))

        sys.exit()

    u=u//2

    for i in range(N):

        if T[i]==t:

            L[i]-=u

        else:

            L[i]+=u

    for x,y,s in K:

        if L[x]+L[y]!=s:

            print((0))

            sys.exit()

    for i in range(N):

        if L[i]<=0:

            print((0))

            sys.exit()

    print((1))

    sys.exit()

a=10**13

c=10**13

#b=-10**13

#d=-10**13

for i in range(N):

    if T[i]==1:

        a=min(a,L[i])

        #b=max(b,L[i])

    else:

        c=min(c,L[i])

        #d=max(d,L[i])

print((max(0,c+a-1)))
from collections import deque
import sys

N, M = list(map(int, input().split()))
table = [[] for i in range(N)]
ans = []
for i in range(M):
    a, b, c = list(map(int, input().split()))
    table[a - 1].append((b - 1, c))
    table[b - 1].append((a - 1, c))
    ans.append((a - 1, b - 1, c))
h = deque()
inf = -(10**13)
h.append(0)
flag = True
visit = [(inf, 0)] * N
visit[0] = (0, 0)
tree = []
while h:
    x = h.popleft()
    for y, d in table[x]:
        if visit[y][0] != inf and visit[y][1] == visit[x][1]:
            flag = False
            tree.append((x, y, d))
        elif visit[y][0] != inf:
            if (visit[x][0] + visit[y][0]) != d:
                print((0))
                sys.exit()
        if visit[y][0] == inf:
            t = (visit[x][1] + 1) % 2
            visit[y] = (d - visit[x][0], t)
            h.append(y)
if flag:
    mi = -inf
    ma = -inf
    for i in range(N):
        if visit[i][1] == 0:
            mi = min(mi, visit[i][0])
        else:
            ma = min(ma, visit[i][0])
    print((max(0, ma + mi - 1)))
    sys.exit()
a, b, c = tree[0]
s = visit[a][1]
t = visit[a][0] + visit[b][0]
w = (c - t) // 2
for i in range(N):
    if visit[i][1] == s:
        x = visit[i][0]
        visit[i] = (x + w, s)
    else:
        x = visit[i][0]
        visit[i] = (x - w, (s + 1) % 2)
for x, y, d in ans:
    if (visit[x][0] + visit[y][0]) != d:
        print((0))
        sys.exit()
for i in range(N):
    if visit[i][0] <= 0:
        print((0))
        sys.exit()
print((1))

import sys
from collections import deque

N, M = list(map(int, input().split()))
table = [[] for i in range(N)]
for i in range(M):
    u, v, s = list(map(int, input().split()))
    table[u - 1].append((v - 1, s))
    table[v - 1].append((u - 1, s))
L = [0] * N
T = [1] * N  # checker
visit = [False] * N
h = deque()
h.append([0, 1])
visit[0] = True
flag = True
K = []
while h:
    x, t = h.popleft()
    for y, s in table[x]:
        if visit[y]:
            if T[y] == T[x]:
                flag = False
                K.append((x, y, s))
            else:
                if L[y] + L[x] != s:
                    print((0))
                    sys.exit()
            continue
        T[y] = -t
        visit[y] = True
        L[y] = s - L[x]
        h.append([y, -t])
if not flag:
    x, y, s = K[0]
    t = T[x]
    u = L[x] + L[y] - s
    if u % 2 != 0:
        print((0))
        sys.exit()
    u = u // 2
    for i in range(N):
        if T[i] == t:
            L[i] -= u
        else:
            L[i] += u
    for x, y, s in K:
        if L[x] + L[y] != s:
            print((0))
            sys.exit()
    for i in range(N):
        if L[i] <= 0:
            print((0))
            sys.exit()
    print((1))
    sys.exit()
a = 10**13
c = 10**13
# b=-10**13
# d=-10**13
for i in range(N):
    if T[i] == 1:
        a = min(a, L[i])
        # b=max(b,L[i])
    else:
        c = min(c, L[i])
        # d=max(d,L[i])
print((max(0, c + a - 1)))

+import sys -import sys -ans = [] -    a, b, c = list(map(int, input().split())) -    table[a - 1].append((b - 1, c)) -    table[b - 1].append((a - 1, c)) -    ans.append((a - 1, b - 1, c)) +    u, v, s = list(map(int, input().split())) +    table[u - 1].append((v - 1, s)) +    table[v - 1].append((u - 1, s)) +L = [0] * N +T = [1] * N  # checker +visit = [False] * N -inf = -(10**13) -h.append(0) +h.append([0, 1]) +visit[0] = True -visit = [(inf, 0)] * N -visit[0] = (0, 0) -tree = [] +K = [] -    x = h.popleft() -    for y, d in table[x]: -        if visit[y][0] != inf and visit[y][1] == visit[x][1]: -            flag = False -            tree.append((x, y, d)) -        elif visit[y][0] != inf: -            if (visit[x][0] + visit[y][0]) != d: -                print((0)) -                sys.exit() -        if visit[y][0] == inf: -            t = (visit[x][1] + 1) % 2 -            visit[y] = (d - visit[x][0], t) -            h.append(y) -if flag: -    mi = -inf -    ma = -inf -    for i in range(N): -        if visit[i][1] == 0: -            mi = min(mi, visit[i][0]) -        else: -            ma = min(ma, visit[i][0]) -    print((max(0, ma + mi - 1))) -    sys.exit() -a, b, c = tree[0] -s = visit[a][1] -t = visit[a][0] + visit[b][0] -w = (c - t) // 2 -for i in range(N): -    if visit[i][1] == s: -        x = visit[i][0] -        visit[i] = (x + w, s) -    else: -        x = visit[i][0] -        visit[i] = (x - w, (s + 1) % 2) -for x, y, d in ans: -    if (visit[x][0] + visit[y][0]) != d: +    x, t = h.popleft() +    for y, s in table[x]: +        if visit[y]: +            if T[y] == T[x]: +                flag = False +                K.append((x, y, s)) +            else: +                if L[y] + L[x] != s: +                    print((0)) +                    sys.exit() +            continue +        T[y] = -t +        visit[y] = True +        L[y] = s - L[x] +        h.append([y, -t]) +if not flag: +    x, y, s = K[0] +    t = T[x] +    u = L[x] + L[y] - s +    if u % 2 != 0: +    u = u // 2 +    for i in range(N): +        if T[i] == t: +            L[i] -= u +        else: +            L[i] += u +    for x, y, s in K: +        if L[x] + L[y] != s: +            print((0)) +            sys.exit() +    for i in range(N): +        if L[i] <= 0: +            print((0)) +            sys.exit() +    print((1)) +    sys.exit() +a = 10**13 +c = 10**13 +# b=-10**13 +# d=-10**13 -    if visit[i][0] <= 0: -        print((0)) -        sys.exit() -print((1)) +    if T[i] == 1: +        a = min(a, L[i]) +        # b=max(b,L[i]) +    else: +        c = min(c, L[i]) +        # d=max(d,L[i]) +print((max(0, c + a - 1)))