#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second

#define DUMP(x) cout<<#x<<":"<<(x)<<endl
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class T>
istream& operator>>(istream& is, vector<T>& xs){
  for(auto& x: xs)
	is >> x;
  return is;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T>& xs){
  for(unsigned int i=0;i<xs.size();++i)
	os << (i?" ":"") << xs[i];
  return os;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}


const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;
const LL INF = 1e18;

using Graph = vector<vector<PII>>;
bool dfs(Graph& G, LL c, int u, int p, int sgn, VVL& ps){
  if(ps[sgn][u] == INF){
	ps[sgn][u] = c;
  }
  else{
	if(ps[sgn][u] != c) return false;
	return true;
  }
  
  for(auto& to: G[u]){
	if(to.FF == p) continue;
	if(!dfs(G, to.SS - c, to.FF, u, 1^sgn, ps))
	  return false;
  }
  return true;
}

bool check(Graph& G, LL x0, int N){
  queue<int> q;
  q.push(0);
  VL val(N, INF);
  val[0] = x0;
  while(!q.empty()){
	int u = q.front();
	q.pop();

	for(auto& e: G[u]){
	  if(val[e.FF] == INF){
		val[e.FF] = e.SS - val[u];
		q.push(e.FF);
	  }
	}
  }

  REP(i,N){
	if(val[i] <= 0) return false;
	for(auto& e: G[i]){
	  if(val[i] + val[e.FF] != e.SS){
		return false;
	  }
	}
  }
  return true;
}

LL naive(Graph& G, int N, int M){
  LL ans = 0;
  return ans;
}

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

  int N, M;
  cin >> N >> M;
  Graph G(N);
  LL sum = 0;
  REP(i,M){
	LL u, v, s;
	cin >> u >> v >> s;
	--u;
	--v;
	G[u].PB(MP(v,s));
	G[v].PB(MP(u,s));
	sum += s;
  }

  VVL ps(2, VL(N, INF));
  if(!dfs(G, 0, 0, -1, 0, ps)){
	cout << 0 << endl;
	return 0;
  }

  LL lb = 1, ub = INF;
  REP(i,N){
	LL mx = INF;
	for(auto& e: G[i])
	  mini(mx, (LL)e.SS);
	  
	if(ps[0][i] != INF){
	  mini(ub, mx - ps[0][i]);
	  maxi(lb, 1 - ps[0][i]);
	}
	if(ps[1][i] != INF){
	  maxi(lb, ps[1][i] - mx + 1);
	  mini(ub, ps[1][i]);
	}
	//cout << ps[0][i]<<","<<ps[1][i]<<endl;
  }
  /*
  for(LL i=lb;i<ub;++i){
	bool c = check(G, i, N);
	if(!c) cout << "FALSE " << i << endl;
  }
  */

  LL ans = max(0ll, ub-lb);
  LL xx = INF;
  REP(i,N){
	if(ps[0][i] != INF && ps[1][i] != INF){
	  LL t = ps[1][i] - ps[0][i];
	  if(t % 2 != 0){
		ans = 0;
		break;
	  }

	  if(xx != INF && xx != t/2){
		ans = 0;
		break;
	  }
	  ans = 1;
	  xx = t / 2;
	}
  }
  if(xx != INF && ans){
	ans = check(G, xx, N);
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 20076