#include <cstring>
#include <map>
#include <unordered_map>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <set>
#include <unordered_set>
using namespace std;

#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int, int> pii;

int gi() {
    int a;
    scanf("%d", &a);
    return a;
}

ll gli() {
    ll a;
#ifndef ONLINE_JUDGE
    scanf("%lld", &a);
#else
    scanf("%I64d", &a);
#endif
    return a;
}

#define L 2147483600
#define EXIT() printf("0\n"); exit(0)

vector<pair<int, ll> > ne[100000];
ll mnx, mxx;
int v[100000];
int resx[100000];
ll resc[100000];

void dfs(int a, int x, ll c) {
    if (v[a])
        return;
    v[a] = 1;
    resx[a] = x;
    resc[a] = c;
    if (x == 1)
        mnx = MAX(mnx, 1LL-c);
    else
        mxx = MIN(mxx, c-1LL);
    vector<pair<int, ll> > &vec = ne[a];
    int l = vec.size();
    for (int i = 0; i < l; i++) {
        pair<int, ll> &p = vec[i];
        int b = p.first;
        ll s = p.second;

        if (v[b]) {
            int rx = resx[b];
            ll tc = resc[b] + c;
            if (rx == x) {
                ll x2 = x*(s-tc);
                if (((x2 % 2)+4)%2) {
                    EXIT();
                }
                x2 /= 2;
                mnx = MAX(mnx, x2);
                mxx = MIN(mxx, x2);
            } else if (tc != s) {
                EXIT();
            }
        } else
            dfs(b, -x, s-c);
    }
}

int main() {
    int n = gi();
    int m = gi();
    for (int i = 0; i < m; i++) {
        int x = gi()-1;
        int y = gi()-1;
        ll s = gi();
        ne[x].push_back(make_pair(y, s));
        ne[y].push_back(make_pair(x, s));
    }

    mnx = 0;
    mxx = 1500000000;
    memset(v, 0, sizeof(v));
    dfs(0, 1, 0);

    cout << (mnx > mxx ? 0LL : mxx-mnx+1LL) << endl;

    return 0;
}
