//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 922337203685477580LL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

struct UnionFind {
  vector<int> par; // 親を指すvector,-par[親]は木のサイズ
  UnionFind(int n):par(n, -1) {} // uniteで親を埋め込んでいく必要あり
  int root(int x) { // 親をたどる&データの整理
    if(par[x] < 0) return x;
    return par[x] = root(par[x]);
  }
  bool unite(int x, int y) { // データの結合
    x = root(x);
    y = root(y);
    if(x == y) return false;
    if(par[x] > par[y]) swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  bool same(int x, int y) {return root(x) == root(y);} // 所属判定
  int size(int x) {return -par[root(x)];} // 木のサイズ
};

struct edge {
  int to, cost;
};
#define N 100100

//head

int n, m;
vector<edge> G[N];
UnionFind uft(N<<1);
bool bip = true;
bitset<N> check, bits;
ll val[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  rep(i, m) {
    int u, v, s;
    cin >> u >> v >> s;
    u--; v--;
    G[u].push_back((edge){v, s});
    G[v].push_back((edge){u, s});
    uft.unite(u, v+N);
    uft.unite(u+N, v);
    if(uft.same(u, u+N)) bip = false;
  }
  if(bip) {
    vector<ll> numbers[2];
    queue<int> q;
    q.emplace(0);
    check.set(0);
    bool ok = true;
    while(!q.empty()) {
      int now = q.front();
      q.pop();
      for(edge &e:G[now]) {
        int ne = e.to;
        if(check.test(ne)) {
          if(val[now]+val[ne] != e.cost) ok = false;
          continue;
        }
        check.set(ne);
        val[ne] = e.cost-val[now];
        q.emplace(ne);
      }
    }
    rep(i, n) {
      numbers[uft.same(n-1, i)].emplace_back(val[i]);
    }
    ll lef = *min_element(all(numbers[0])), righ = *min_element(all(numbers[1]));
    if(ok) cout << max((ll)0, lef+righ-1) << endl;
    else cout << "0\n";
  }
  else {
    queue<int> q;
    q.emplace(0);
    check.set(0);
    bool ok = true;
    while(!q.empty()) {
      int now = q.front();
      q.pop();
      for(edge &e:G[now]) {
        int ne = e.to;
        if(check.test(ne)) {
          if((bits.test(now) and bits.test(ne)) or (!bits.test(now) and !bits.test(ne))) {
            if((val[now]+val[ne]+e.cost)&1) {
              cout << "0\n";
              return 0;
            }
            check = 0;
            check.set(now);
            while(!q.empty()) q.pop();
            q.emplace(now);
            val[now] = (val[now]+e.cost-val[ne])>>1;
            while(!q.empty()) {
              int now = q.front();
              q.pop();
              if(val[now] < 1) {
                cout << "0\n";
                return 0;
              }
              for(edge &e:G[now]) {
                int ne = e.to;
                if(check.test(ne)) {
                  if(val[now]+val[ne] != e.cost) {
                    cout << "0\n";
                    return 0;
                  }
                  continue;
                }
                check.set(ne);
                val[ne] = e.cost-val[now];
                q.emplace(ne);
              }
            }
            cout << "1\n";
            return 0;
          }
          else {
            if(val[now]+val[ne] != e.cost) {
              cout << "0\n";
              return 0;
            }
          }
          continue;
        }
        if(!bits.test(now)) bits.set(ne);
        val[ne] = e.cost-val[now];
        check.set(ne);
        q.emplace(ne);
      }
    }
  }
}

Measured memory_bytes: 13036