#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cmath>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
template<class T> inline bool chmax(T& a, T b) {if (a<b) {a=b; return true;} return false;}
template<class T> inline bool chmin(T& a, T b) {if (a>b) {a=b; return true;} return false;}

const ll MOD = 1000000007;
const ll HIGHINF = (ll)1e18;

#define BLACK 0
#define WHITE 1

V<vvll> edges;
vll d, bw;
ll bmin = HIGHINF, wmin = HIGHINF, tmp;
bool change = false;

bool dfs(ll i) {
  if (bw[i] == BLACK) chmin(bmin, d[i]);
  else chmin(wmin, d[i]);
  for (auto toe: edges[i]) {
    ll s = toe[0], to = toe[1];
    if (bw[to] == -1) {
      bw[to] = (bw[i] + 1) % 2;
      d[to] = s - d[i];
      if (!dfs(to)) return false;
    }
    if (bw[i] != bw[to]) {
      // 辺の色が違う時は和が一定なのでsにならないといけない
      if (d[i] + d[to] != s) return false;
    } else {
      ll val = (bw[i]==BLACK?1:-1) * (s-d[i]-d[to]);
      if (!change) {
        tmp = val; change=true;
      }
      if (val % 2 == 1 || tmp != val) {  // どれだけ変えるかがマッチしないなら
        return false;
      }
    }
  }
  return true;
}

int main() {
  ll n, m; cin >> n >> m;
  edges.resize(n);
  REP(i, m) {
    ll u, v, s; cin >> u >> v >> s;
    vll sv(2); sv[0] = s; sv[1] = v-1;
    edges.at(u-1).push_back(sv);
    sv[1] = u-1;
    edges.at(v-1).push_back(sv);
  }
  bw.resize(n); REP(i, n) bw[i] = -1; bw[0] = 0;  // BLACK: 0, WHITE: 1
  d.resize(n); REP(i, n) d[i] = 0;
  if (!dfs(0)) {
    cout << 0 << endl;
  } else {
    if (change) {
      if (tmp/2+bmin<=0 || wmin+tmp/2<=0) cout << 0 << endl;
      else cout << 1 << endl;
    } else {
      cout << max(0LL, wmin+bmin-1) << endl;
    }
  }
  return 0;
}
