#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}

const int MAXN = 100011;
int N, M;
vector<pair<int, LL> > G[MAXN];

int par[MAXN];
LL par_cost[MAXN];
int color[MAXN];
VI ord;

bool bipartite() {
    ord.clear();
    ord.reserve(N);
    ord.push_back(0);
    memset(color, -1, sizeof color);
    color[0] = 0;
    REP (i, N) {
	int v = ord[i];
	EACH (e, G[v]) if (color[e->first] == -1) {
	    color[e->first] = 1 - color[v];
	    ord.push_back(e->first);
	    par[e->first] = v;
	    par_cost[e->first] = e->second;
	}
    }

    REP (i, N) EACH (e, G[i]) if (color[i] == color[e->first]) return false;
    return true;
}

const LL INF = 1LL<<60;
LL value[MAXN];

void MAIN() {
    scanf("%d%d", &N, &M);
    REP (i, M) {
	int x, y, s;
	scanf("%d%d%d", &x, &y, &s);
	x--; y--;
	G[x].emplace_back(y, s);
	G[y].emplace_back(x, s);
    }

    if (bipartite()) {
	REP (i_, N) {
	    int v = ord[i_];
	    EACH (e, G[v]) {
		value[e->first] = e->second - value[v];
	    }
	}
	bool yes = true;
	REP (i_, N) {
	    int v = ord[i_];
	    EACH (e, G[v]) {
		if (value[e->first] + value[v] != e->second) yes = false;
	    }
	}

	LL ans = 0;
	if (yes) {
	    LL mi[2] = { INF, INF };
	    REP (v, N) {
		amin(mi[color[v]], value[v]);
	    }

	    LL diff = 1 - mi[0];
	    mi[0] += diff;
	    mi[1] -= diff;
	    if (mi[1] >= 1) {
		ans = mi[1];
	    }
	} else {
	    ans = 0;
	}

	printf("%lld\n", ans);
    } else {
	int X = 0, Y = 0;
	LL S = 0;
	REP (i, N) EACH (e, G[i]) if (color[i] == color[e->first]) {
	    X = i;
	    Y = e->first;
	    S = e->second;
	    goto BREAK;
	}
BREAK:

	LL Ax = 1, Bx = 0;
	int cur = X;
	while (cur) {
	    Ax = -Ax;
	    Bx = par_cost[cur] - Bx;
	    cur = par[cur];
	}

	LL Ay = -1, By = S;
	cur = Y;
	while (cur) {
	    Ay = -Ay;
	    By = par_cost[cur] - By;
	    cur = par[cur];
	}

	int ans = 0;

	if ((By - Bx) % (Ax - Ay) == 0) {
	    LL Xval = (By - Bx) / (Ax - Ay);
	    if (Xval > 0) {
		REP (i, N) value[i] = INF;
		ord.clear();
		ord.push_back(X);
		value[X] = Xval;
		REP (i_, N) {
		    int v = ord[i_];
		    EACH (e, G[v]) if (value[e->first] == INF) {
			value[e->first] = e->second - value[v];
			ord.push_back(e->first);
		    }
		}
//		eprintf("%d\n", (int)ord.size());

		bool yes = (*min_element(value, value + N) > 0);
		if (yes) {
		    REP (i, N) EACH (e, G[i]) if (value[i] + value[e->first] != e->second) yes = false;
		}

		ans = (yes? 1: 0);
	    }
	}
	printf("%d\n", ans);
    }
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}



Measured memory_bytes: 10752