#include <iostream>
#include <vector>
#include <utility>
#include <queue>

using namespace std;
const int MAX = 100010;
long long n,m,dist[MAX],used[MAX],a[MAX],color[MAX];
struct edge{long long to,cost;};
pair<pair<int,int>,long long> E[MAX];
vector<edge> G[MAX];

void dfs(int s,int x,int y){
    a[s] = y; dist[s] = x;used[s] = true;
    for(int i=0;i<G[s].size();i++){
        if(!used[G[s][i].to]) dfs(G[s][i].to,x+1,G[s][i].cost-y);
    }
}

bool bip(int i, int col) {
    if (color[i] == col) return true;
    if (color[i] != 0) return false;
    bool ok = true;
    color[i] = col;
    for (int u = 0;u<G[i].size();u++)
        ok &= bip(G[i][u].to, 3 - col);
    return ok;
}

bool check(){
    for(int i=0;i<m;i++){
        if(a[E[i].first.first]+a[E[i].first.second]!=E[i].second){
            return false;
        }
    }
    return true;
}

vector<edge> c,d;
long long dep[MAX],ml=0,l;

void cycle(int s,int dist){
	dep[s] = dist;
	for(edge e : G[s]){
		if(dep[e.to]==-1) {
			c.push_back(e);
			cycle(e.to,dist+1);
		}else if(dep[e.to]<dep[s]-1 && !(ml & 1)){
			ml = dep[s]+1-dep[e.to];
			if(ml & 1){
				c.push_back(e);
				l = e.to;
				for(int j =0; j<c.size();j++){
					edge p = c[j];
					d.push_back(p);
				}
			}
		}
	}
	if(!(ml & 1)){
		c.pop_back();
	}
}

int main(){
    int i;
    cin >> n >> m;
    for(i=0;i<m;i++){
        long long u,v,s;
        cin >> u >> v >> s;
        u--; v--;
        edge e; e.to = v; e.cost = s;
        G[u].push_back(e);
        e.to = u; e.cost = s;
        G[v].push_back(e);
        pair<int,int> p = make_pair(u,v);
        E[i] = make_pair(p,s);
    }
    long long min1 = 1000000000,min2 = 10000000000;
    dfs(0,0,0);
    for(i=0;i<n;i++){
        if(dist[i]%2==1){
            min1 = min(min1,a[i]);
        }else{
            min2 = min(min2,a[i]);
        }
    }
    if(bip(0,1)){
        if(!check()){
            cout << 0 << endl;
            return 0;
        }
        if(min1<=0){
            cout << 0 << endl;
        }else if(min1>0 && min2>=0){
            cout << min1-1 << endl;
        }else if(min1>=0 && min2<0){
            if(min1+min2<=0){
                cout << 0 << endl;
            }else{
            	cout << min1+min2-1 << endl;
            }
        }
    }else{
 		 /*struct data { long long a0; int sign; };
 		 vector<data> t(n);
 		 for (auto&& i : t) {
   			 i.sign = 0;
 		 }
 		 queue<int> q;
  		t[0].a0 = 0;
  		t[0].sign = 1;
 		q.push(0);
  		bool fixed = false;
  		long long s = 0;
  		while (not q.empty()) {
    		int v = q.front(); q.pop();
   				 for (auto&& e : G[v]) {
      				if (t[e.to].sign == 0) {
        				t[e.to].sign = t[v].sign * -1;
        				t[e.to].a0 = e.cost - t[v].a0;
        				q.push(e.to);
      				} else {
        				if (!fixed) {
          					if (t[v].sign != t[e.to].sign) {
            					if (t[v].a0 + t[e.to].a0 != e.cost) {
              						cout << 0 << '\n';
              						return 0;
           						}
          					} else {
           						 s = t[v].sign * (e.cost - t[v].a0 - t[e.to].a0);
            							if (s & 1) {
              								cout << 0 << '\n';
              								return 0;
            							} else {
              								s /= 2;
            }
            fixed = true;
          }
        } else {
          if (t[v].a0 + t[v].sign * s + t[e.to].a0 + t[e.to].sign * s != e.cost) {
            cout << 0 << '\n';
            return 0;
          }
        }
      }
    }
  }*/

  /*if (fixed == false) {
    lint mi = 1e18, mi2 = 1e18;
    for (int i = 0; i < n; i++) {
      if (a[i].sign == 1) {
        mi2 = min(mi2, a[i].a0);
      } else {
        mi = min(mi, a[i].a0);
      }
    }
    if (mi <= 1 or mi2 + mi <= 0) {
      cout << 0 << '\n';
    } else {
      cout << mi + mi2 - 1 << '\n';
    }
    	} elseif(fixed) {
    for (int i = 0; i < n; i++) {
      if (t[i].a0 + t[i].sign * s <= 0) {
        cout << 0 << '\n';
        break;
      }
      if (i == n - 1) {
        cout << 1 << '\n';
      }
    }
  }
  return 0;*/

   	for(i=0;i<n;i++){
        dep[i] = -1;
   		used[i] = false;
    }
    cycle(0,0);
    int x = 1,k = d.size() - ml;
    long long sum = 0;
    	//cout << c.size() << " " << ml <<  endl;
    for(i=d.size()-1;i>=k;i--){
    	sum += x*d[i].cost;
    	x *= -1;
    }
    	//cout << c[0].cost << " " << c[1].cost << " " << c[2].cost << " " << endl;
    	//cout << sum << " " << l << endl;
    if(sum%2==1){
    	cout << 0 << endl;
    	return 0;
    }
    dfs(l,0,sum/2);
    for(i=0;i<n;i++){
    	min1 = min(min1,a[i]);
    }
    if(check() && min1>0){
        	cout << 1 << endl;
        }else{
            cout << 0 << endl;
        }
    }
}


Measured memory_bytes: 22900