#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define debug_print(var) cerr << #var << "=" << var <<endl;
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "
#define fi first
#define se second

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c;};

ll mod(ll a, ll mod){
  ll res = a%mod;
  if(res<0)res=res + mod;
  return res;
}

ll modpow(ll a, ll n, ll mod){
  ll res=1;
  while(n>0){
    if(n&1) res=res*a%mod;
    a=a*a%mod;
    n>>=1;
  }
  return res;
}

ll modinv(ll a, ll mod){
  ll b=mod, u=1, v=0;
  while(b){
    ll t=a/b;
    a-=t*b; swap(a, b);
    u-=t*v; swap(u, v);
  }
  u%=mod;
  if(u<0)u+=mod;
  return u;
}

ll gcd(ll a, ll b){
  ll r = a%b;
  if(r==0) return b;
  else return gcd(b, a%b);
}

bool is_prime(ll n){
  ll i = 2;
  if(n==1)return false;
  if(n==2)return true;
  bool res = true;
  while(i*i <n){
    if(n%i==0){
      res = false;
    }
    i = i+1;
  }

  //if(i==1)res = false;
  if(n%i==0)res=false;
  return res;
}

struct form{//  b + k * x 
  ll b, k;
};


/**************************************
** A main function starts from here  **
***************************************/
int main(){
  ll N, M;
  cin >> N >> M;
  vector<vector<edge>> e(N);
  rep(i, M){
    ll u, v, s;
    cin >> u >> v >> s;
    u--;v--;
    e[u].push_back({v, s});
    e[v].push_back({u, s});
  }
  
  vector<form> v(N, {0, -2});
  v[0] = {0, 1};
  //v[0][0] = {0, -1};
  queue<P> q;
  q.push({0, -1});
  bool impossible = false;
  set<ll> ans;
  while(!q.empty()){
    auto node = q.front();
    q.pop();
    ll iq = node.first;
    ll from = node.second;
    for(auto iedge: e[iq]){
      ll next = iedge.x;
      if(next==from)continue;
      ll cost = iedge.c;
      if(v[next].k==-2){
        v[next] = {cost - v[iq].b, -v[iq].k};
        q.push({next, iq});
      }else{
        ll nextb = cost  - v[iq].b;
        ll nextk = - v[iq].k;
        
        if(nextk==v[next].k and nextb != v[next].b){
          impossible = true;
        }
        
        if(nextk!=v[next].k){
          if(nextk<0){
            ll a = nextb - v[next].b;
            
            if(a%2!=0){impossible=true;}
            if(a<=0){impossible=true;}

            a = a/2;
            ans.insert(a);
          }
          if(nextk>0){
            ll a = -nextb + v[next].b;
            if(a%2!=0)impossible=true;
            if(a<=0)impossible=true;
            
            a = a/2;
            ans.insert(a);
          }

        }
      }
    }
  }

  if(impossible){
    cout << 0;
  }else{
    if(ans.size()>1){
      cout << 0;
    }else{
      ll maxv = 1e18;
      ll minv = -1e18;
      for(auto iv: v){
        if(iv.k<0){
          maxv = min(maxv, iv.b-1);
        }
        if(iv.k>0){
          minv = max(minv, -iv.b);
        }
      }
      if(maxv<=minv){
        cout << 0;
      }else{
        if(ans.size()==0){
          cout << maxv-max(minv, 0LL);
        }else{
          ll a = *ans.begin();
          if(a>minv and maxv>=a)cout << 1;
          else cout << 0;
        }
      }
    }
  }
  
  
  return 0;
}


Measured memory_bytes: 13484