#include "bits/stdc++.h"

using namespace std;

using ll = long long;

template <typename T>
void printv(const vector<T> &v) {
  int sz = v.size();
  for (int i = 0; i < sz; i++) {
    cout << v[i] << " \n"[i == sz - 1];
  }
}

struct Edge {
  int to;
  ll s;
};

using Graph = vector<vector<Edge>>;
const int MAX_N = 100010;
const ll inf = 1e15;
vector<vector<ll>> dp(MAX_N, vector<ll>(2, inf));
Graph g(MAX_N);
int n, m;
bool f = true;

void rec(int cur, int flag, ll val) {
  if (dp[cur][flag] == inf) {
    dp[cur][flag] = val;
    for (const auto &e : g[cur]) {
      rec(e.to, 1 - flag, e.s - val);
    }
  }
  if (dp[cur][flag] != val) {
    f = false;
    return;
  }
}

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

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, s;
    cin >> u >> v >> s;
    u--;
    v--;
    g[u].push_back(Edge{v, s});
    g[v].push_back(Edge{u, s});
  }
  rec(0, 0, 0);
  //for(int i=0;i<n;i++){
  //    cout << "i: " << i+1 << " +a:" << dp[i][0] << " -a: " << dp[i][1] << endl;
  //}
  if (!f) {
    cout << 0 << endl;
    return 0;
  }
  // bipatite
  if (dp[0][1] == inf) {
    ll low = 1, high = inf;
    for (int i = 1; i < n; i++) {
      if (dp[i][0] != inf) {
        low = max(low, -dp[i][0] + 1);
      } else {
        high = min(high, dp[i][1] - 1);
      }
    }
    //cout << "high: " << high << " low: " << low << endl;
    cout << max(0LL, high - low + 1) << endl;
    return 0;
  }
  ll tmp = dp[0][1] - dp[0][0];
  if (tmp % 2) {
    cout << 0 << endl;
    return 0;
  }
  tmp /= 2;
  for (int i = 0; i < n; i++) {
    ll ret1 = tmp + dp[i][0], ret2 = -tmp + dp[i][1];
    if (ret1 <= 0 || ret2 <= 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  cout << 1 << endl;
}
