/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include<bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
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; }

using ll = long long;
using P = pair<ll, ll>;

constexpr double PI = 3.14159265358979323846;
mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count());

class EGraph {
public:

    vector<P> graph[100005];
    vector<P> lim;
    vector<bool> used;
    ll ans1 = 0;
    ll ans2 = 0;
    ll k = 0;

    void dfs(int p, P l) {
      debug(p, l.first, l.second);
      used[p] = true;
      l.first *= -1;
      for(auto e: graph[p]) {
        auto s = e.second-l.second;
        auto q = e.first;
        if (lim[q].first == 0) {
          lim[q].first = l.first;
          lim[q].second = s;
          dfs(q, lim[q]);
        } else if (lim[q].first != l.first) {
          ans2 = -1;
          int r = s-lim[q].second;
          if (l.first != -1) {
            r = -r;
          }
          debug(r);
          if (r%2==0 && r>0) {
            k = r/2;
          } else {
            ans1 = -1;
          }
        } else {
          if (lim[q].second != s) {
            ans1 = -1;
          }
        }
      }
    }

    vector<int> c;
    int ans3 = 1;
    void dfs2(int p, int r) {
      used[p] = true;
      for(auto e: graph[p]) {
        int q = e.first;
        int s = e.second-r;
        if (s <= 0) ans3 = 0;
        if (c[q] == -1) {
          c[q] = s;
          dfs2(q, s);
        } else {
          if (c[q] != s) ans3 = 0;
        }
      }
    }

    void solve(istream& cin, ostream& cout) {
      SPEED;
      int n, m; cin >> n >> m;
      lim.resize(n); used.resize(n);
      rep(i, m) {
        int a, b, c; cin >> a >> b >> c;
        a--; b--;
        graph[a].emplace_back(b, c);
        graph[b].emplace_back(a, c);
      }

      lim[0] = P(1, 0);
      dfs(0, lim[0]);

      if (ans1 == -1) {
        cout << 0 << endl;
        return;
      }
      if (ans2 == -1) {
        c.resize(n, -1);
        rep(i, n) used[i] = false;
        assert(k>0);
        debug(k);
        c[0] = k;
        dfs2(0, k);
        cout << ans3 << endl;
        return;
      }

      ll mi = 1;
      ll ma = LLONG_MAX;
      rep(i, n) {
        debug(lim[i].first, lim[i].second);
        if (lim[i].first == 1) {
          chmax(mi, -lim[i].second+1);
        } else {
          chmin(ma, lim[i].second-1);
        }
      }
      cout << max(0LL, ma-mi+1) << endl;
    }
};

signed main() {
  EGraph solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}

Measured memory_bytes: 12160