#include <ext/pb_ds/assoc_container.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
template <class c, class cmp = less<c> > using ordered_set = tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...) 
#endif
template<class T> ostream& operator<<(ostream &os, vector<T> V) {os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {return os << "(" << P.first << "," << P.second << ")";}
template <typename T,typename U>pair<T,U> operator+(const pair<T,U> & l,const std::pair<T,U> & r) { return {l.first+r.first,l.second+r.second};}
typedef long long int ll;
const ll mod =1e18;
const int maxn = 300002;
#define endl '\n'
#define int ll
#define ld long double
#define all(x) (x).begin(),(x).end()
struct edge{
  int v,s,i;
};
vector<edge> g[maxn];
int ans[maxn];
int vis[maxn],lev[maxn];
set<int> tr;
void dfs(int u ,int d = 0){
  ans[u] = d;
  vis[u] = true;
  for(edge e:g[u]){
    if(vis[e.v]) continue;
    lev[e.v] = lev[u] + 1;
    dfs(e.v,e.s-d);
    
    tr.insert(e.i);
  }
}

int32_t main()
{ 
  IOS

  int n,m;
  cin >> n >> m;
  vector<edge> e(m+1);
  for(int i = 1; i<=m;i++){
    int u,v,s;
    cin >> u >> v >> s;
    g[u].push_back({v,s,i});
    g[v].push_back({u,s,i});
    e[i] = edge({u,v,s});
  }
  dfs(1,0);
  set<int> candi;
  for(int i = 1;i<=m;i++){
    if(!tr.count(i)){
      if(((lev[e[i].v]%2) != (lev[e[i].s]%2))){
          if((ans[e[i].v] + ans[e[i].s] != e[i].i)){
            cout<<0<<endl;
            return 0;
          }
          
      }
      else{
        if(lev[e[i].v]%2 == 0){
          int sn = e[i].i - ans[e[i].v]- ans[e[i].s];
          if(sn<=0 || (sn%2)){
            cout<<0<<endl;
            return 0;
          }
          candi.insert(sn/2);
        }
        else {
          int sn = e[i].i - ans[e[i].v]- ans[e[i].s];
          sn*=-1;
          if(sn<=0 || (sn%2)){
            cout<<0<<endl;
            return 0;
          }
          candi.insert(sn/2);
        }
      }
    }
  }

  if(candi.size()>1){
    cout<<0<<endl;
    return 0;
  } 
  int mini = 1,maxi = 1e9;
  for(int i = 1;i<=n;i++){
    if(lev[i]%2 == 0){
      if(ans[i]<0){
        mini = max(mini,-ans[i] + 1);
      }
    }
    else{
      if(ans[i]<=0){
        cout<<0<<endl;
        return 0;
      }
      else{
        maxi = min(maxi, ans[i] - 1);
      }
    }
  }
  if(mini>maxi){
    cout<<0<<endl;
    return 0;
  }
  if(candi.size() == 1){
    if(*candi.begin()>=mini and *candi.begin()<=maxi){
      cout<<1<<endl;
      return 0;
    }
    else{
      return 0;
    }
  }
  cout<<max(0ll,maxi - mini + 1)<<endl;
  
}

Measured memory_bytes: 37008