#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef complex<double> xy_t;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
ll inf = llmax / 2;
double eps = 1e-11;

struct edge {
    int to, cost;
};

int V;
vector<edge> G[1000000];
ll d[1000000];

bool bipartite() {
    fill(d, d + V, -1);
    int num = 0;
    while (true) {
        for (int i = num; i < V; i++) {
            if (d[num] == -1) {
                break;
            }
            num++;
        }
        if (num == V) {
            return true;
        }
        d[num] = 0;
        queue<int> que;
        que.push(num);
        while (que.size() > 0) {
            int base = que.front();
            que.pop();
            int s = G[base].size();
            for (int i = 0; i < s; i++) {
                int to = G[base][i].to;
                if (d[to] == -1) {
                    if (d[base] == 0) {
                        d[to] = 1;
                    } else {
                        d[to] = 0;
                    }
                    que.push(to);
                } else {
                    if (d[to] == 1 && d[base] == 1) {
                        return false;
                    } else if (d[to] == 0 && d[base] == 0) {
                        return false;
                    }
                }
            }
        }
    }
}

ll nums[1000000];
ll err[2];

int put(int st) {
    fill(nums, nums + V, -1);
    int num = 0;
    while (true) {
        for (int i = num; i < V; i++) {
            if (nums[num] == -1) {
                break;
            }
            num++;
        }
        if (num == V) {
            return -1;
        }
        nums[num] = st;
        queue<int> que;
        que.push(num);
        while (que.size() > 0) {
            int base = que.front();
            que.pop();
            int s = G[base].size();
            for (int i = 0; i < s; i++) {
                int to = G[base][i].to;
                int cost = G[base][i].cost;
                if (nums[to] == -1) {
                    nums[to] = cost - nums[base];
                    que.push(to);
                } else {
                    if (nums[to] != cost - nums[base]) {
                        err[0] = nums[to];
                        err[1] = cost - nums[base];
                        return to;
                    }
                }
            }
        }
    }
}

int main() {
    int n, m;
    cin >> n >> m;
    V = n;
    for (int i = 0; i < m; i++) {
        int u, v, s;
        cin >> u >> v >> s;
        edge e1;
        e1.to = v - 1;
        e1.cost = s;
        G[u - 1].push_back(e1);
        edge e2;
        e2.to = u - 1;
        e2.cost = s;
        G[v - 1].push_back(e2);
    }
    bool ok = bipartite();
    if (ok) {
        int p = put(1);
        if (p != -1) {
            cout << 0 << endl;
            return 0;
        } else {
            ll upmin = 1;
            ll downmin = llmax;
            for (int i = 0; i < n; i++) {
                if (d[i] == 0) {
                    if (nums[i] < upmin) {
                        upmin = nums[i];
                    }
                } else {
                    if (nums[i] < downmin) {
                        downmin = nums[i];
                    }
                }
            }
            cout << max(0ll, downmin - (1 - upmin)) << endl;
        }
    } else {
        int p = put(1);
        if (p == -1) {
            cout << 1 << endl;
            return 0;
        } else {
            ll er = abs(err[1] - err[0]);
            if (er % 2 == 1) {
                cout << 0 << endl;
                return 0;
            } else {
                int q = put(1 + er / 2);
                if (q == -1) {
                    bool okk = true;
                    for (int i = 0; i < n; i++) {
                        if (nums[i] < 1) {
                            okk = false;
                        }
                    }
                    if (okk) {
                        cout << 1 << endl;
                    } else {
                        cout << 0 << endl;
                    }
                    return 0;
                } else {
                    cout << 0 << endl;
                    return 0;
                }
            }
        }
    }
}


Measured memory_bytes: 34164