#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
#include<algorithm>
#include<functional>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<deque>
#include<tuple>
#include<iostream>
#include<string>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define REP(i,x) for (int i=0;i<x;i++)
#define REPP(i,x,y) for (int i=x;i<=y;i++)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define MX3(x,y,z) max({x,y,z})
#define MX4(x,y,z,p) max({x,y,z,p})
#define MX5(x,y,z,p,q) max({x,y,z,p,q})
#define MN3(x,y,z) min({x,y,z})
#define MN4(x,y,z,p) min({x,y,z,p})
#define MN5(x,y,z,p,q) min({x,y,z,p,q})
#define amax(x,y) x=max(x,y)
#define amin(x,y) x=min(x,y)
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vb> vvb;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef tuple<int,int,int> ti;
typedef tuple<ll,ll,ll> tl;
// #####################################################################

const int MAX = 100000 + 10;

int color[MAX];
long long val[MAX];
bool vis[MAX];

vector<pair<int,long long>> v[MAX];

bool has_sol = true;
bool conflict = false;
long long des = 0;

void dfs(int x) {
    for(auto e: v[x]) {
        if(color[e.first] != 0) {
            if(color[x] == color[e.first]) {
                // conflict, try to solve equation
                conflict = true;
                if(color[x] == 1) {
                    des = e.second - val[x] - val[e.first];
                    if(des % 2 != 0) has_sol = false;
                    des /= 2;
                } else {
                    des = e.second - val[x] - val[e.first];
                    if(des % 2 != 0) has_sol = false;
                    des /= 2;
                    des = -des;
                }
                return;
            } else {
                if(e.second - val[x] != val[e.first]) {
                    has_sol = false;
                    return;
                }
            }
        } else {
            color[e.first] = 3 - color[x];
            val[e.first] = e.second - val[x];
            dfs(e.first);
        }
        if(has_sol == false || conflict == true) return;
    }
}

void _dfs(int x) {
    vis[x] = true;
    for(auto e: v[x]) {
        if(vis[e.first]) {
            if(val[e.first] != e.second - val[x]) has_sol = false;
        } else {
            val[e.first] = e.second - val[x];
            _dfs(e.first);
        }
        if(!has_sol) return;
    }
}

void solve() {

    int n, m;
    SI(n); SI(m);
    REP(i, m) {
        int x, y, s;
        SI(x);
        SI(y);
        SI(s);
        v[x].push_back(make_pair(y, s));
        v[y].push_back(make_pair(x, s));
    }
    color[1] = 1;
    val[1] = 0L;
    dfs(1);

    if(!has_sol) puts("0");
    else {
        if(conflict) {
            val[1] = des;
            _dfs(1);
            REPP(i, 1, n) {
                if(val[i] <= 0) has_sol = false;
            }
            if(!has_sol) puts("0");
            else puts("1");
        } else {
            ll mmax = 1000000000;
            ll mmin = 1;
            REPP(i, 1, n) {
                if(color[i] == 1) {
                    mmin = -val[i]+1 > mmin ? -val[i]+1 : mmin;
                } else {
                    mmax = val[i]-1 < mmax ? val[i]-1 : mmax;
                }
            }
            if(mmax < mmin) puts("0");
            else printf("%lld\n", mmax-mmin+1);
        }
    }
}

void init() {
    


}

int main () {
    init();
    solve();
    return 0;
}




Measured memory_bytes: 13696