#include <bits/stdc++.h>

#define PB push_back
#define MP make_pair
#define F first
#define S second

using namespace std;

typedef long long ll;

const int max_n = 100111;
ll inf = 1000111222LL * 1000111222LL;

// to, weight
vector<pair<int, int> > v[max_n];
int col[max_n];
pair<int, int> pr[max_n];
int n, m;

int rev(int col) {
    return col == 1 ? 2 : 1;
}

vector<int> cycle;

///////////////
ll s[max_n];
bool vis[max_n];

void dfs_check(int cur) {
    vis[cur] = 1;
    for (int i = 0; i < v[cur].size(); ++i) {
        int to = v[cur][i].F;
        ll w = v[cur][i].S;
        if (vis[to]) continue;
        s[to] = w - s[cur];
        dfs_check(to);
    }
}

bool check(int ver, int val) {
    memset(s, 0, sizeof(s));
    memset(vis, 0, sizeof(vis));
    s[ver] = val;
    dfs_check(ver);
    for (int i = 0; i < n; ++i) {
        if (s[i] <= 0) return false;
        for (int j = 0; j < v[i].size(); ++j) {
            int to = v[i][j].F;
            ll w = v[i][j].S;
            if (s[i] + s[to] != w) return false;
        }
    }
    return true;
}

void restore(int cur, int fst) {
    vector<int> w;
    for (int i = 0; i < v[cur].size(); ++i) {
        int to = v[cur][i].F;
        if (to == fst) {
            w.PB(v[cur][i].S);
            break;
        }
    }
    while (cur != fst) {
        int i = pr[cur].S;
        cur = pr[cur].F;
        w.PB(v[cur][i].S);
    }
    ll sum = 0;
    ll k = 1;
    for (int i = 0; i < w.size(); ++i) {
        sum += w[i] * k;
        k *= -1;
    }
    if (sum <= 0 || sum % 2 == 1 || check(fst, sum / 2) == 0) {
        cout << 0;
        exit(0);
    }
    cout << 1;
    exit(0);
}

void dfs1(int cur, int cur_c) {
    col[cur] = cur_c;
    for (int i = 0; i < v[cur].size(); ++i) {
        int to = v[cur][i].F;
        if (col[to] == 0) {
            pr[to] = {cur, i};
            dfs1(to, rev(cur_c));
            //cout << "pr[" << to << "]=" << cur << endl;
        }
        if (col[to] == col[cur]) {
            restore(cur, to);
        }
    }
}

/////////////
int d[max_n];
ll sum1[max_n]; // +
ll sum2[max_n]; // -

void dfs2(int cur, int k1, int k2) {
    vis[cur] = 1;
    for (int i = 0; i < v[cur].size(); ++i) {
        int to = v[cur][i].F;
        if (vis[to]) continue;
        ll w = v[cur][i].S;
        sum1[to] = sum1[cur] + w * k1;
        sum2[to] = sum2[cur] + w * k2;
        d[to] = d[cur] + 1;
        dfs2(to, -1 * k1, -1 * k2);
    }
}

bool check_edge(int from, int to, ll w) {
    ll sum = sum1[from] + sum2[to];
    if (d[from] % 2 == 0) {
        sum += w;
    } else {
        sum -= w;
    }
    return sum == 0;
}

int main() {
    //ios_base::sync_with_stdio(0);
    //cin.tie(0);
    //cout.tie(0);

    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int a, b, w;
        cin >> a >> b >> w;
        --a, --b;
        v[a].PB({b, w});
        v[b].PB({a, w});
    }
    pr[0] = {-1, -1};
    dfs1(0, 1);

    memset(vis, 0, sizeof(vis));
    sum1[0] = sum2[0] = 0;
    d[0] = 0;
    dfs2(0, 1, -1);

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < v[i].size(); ++j) {
            int to = v[i][j].F;
            ll w = v[i][j].S;
            if (!check_edge(i, to, w)) {
                cout << 0;
                exit(0);
            }
        }
    }

    ll minx = -inf, maxx = inf;
    for (int i = 0; i < n; ++i) {
        if (d[i] % 2 == 0) {
            minx = max(minx, sum1[i] + 1);
        } else {
            maxx = min(maxx, sum1[i] - 1);
        }
    }
    ll ans = (maxx - minx) + 1;
    cout << max(0LL, ans);
    return 0;
}


Measured memory_bytes: 18296