// ===== 2018/07/07 21:34:08 =====

// ../../../lib/template/includes.hpp

#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <vector>

// ../../../lib/template/cin.cpp

template <typename T, int M, int N> struct tuple_getter {
  static void get(std::istream &is, T &x) {
    is >> std::get<M>(x);
    tuple_getter<T, M + 1, N>::get(is, x);
  }
};

template <typename T, int N> struct tuple_getter<T, N, N> {
  static void get(std::istream &is, T &x) { is >> std::get<N>(x); }
};

template <typename... Args>
std::istream &operator>>(std::istream &is, std::tuple<Args...> &t) {
  tuple_getter<std::tuple<Args...>, 0, sizeof...(Args) - 1>::get(is, t);
  return is;
}

template <typename T, typename U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
  for (T &x : v) is >> x;
  return is;
}

// ../../../lib/template/const_value.hpp

namespace sub {
template <typename T>
constexpr typename std::enable_if<std::is_integral<T>::value, T>::type
inf_sub() {
  return std::numeric_limits<T>::max() / 2 - 1000;
}

template <typename T>
constexpr typename std::enable_if<std::is_floating_point<T>::value, T>::type
inf_sub() {
  return std::min(std::numeric_limits<T>::max() / 2 - 1000, T(1e50));
}
}  // namespace sub

template <typename T> constexpr T inf() {
  static_assert(std::is_arithmetic<T>::value, "T must be arithmetic value");
  return sub::inf_sub<T>();
}

template <typename T> constexpr T zero() {
  static_assert(std::is_arithmetic<T>::value, "T must be arithmetic value");
  return T(0);
}

const long double pi = acos(-1.0);

// ../../../lib/template/cout.cpp

template <typename T, int M, int N> struct tuple_printer {
  static void print(std::ostream &os, const T &x) {
    os << std::get<M>(x) << " ";
    tuple_printer<T, M + 1, N>::print(os, x);
  }
};

template <typename T, int N> struct tuple_printer<T, N, N> {
  static void print(std::ostream &os, const T &x) { os << std::get<N>(x); }
};

template <typename... Args>
std::ostream &operator<<(std::ostream &os, const std::tuple<Args...> &t) {
  tuple_printer<std::tuple<Args...>, 0, sizeof...(Args) - 1>::print(os, t);
  return os;
}

template <typename T, typename U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
  os << p.first << " " << p.second;
  return os;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << " ";
    os << *it;
  }
  return os;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::set<T> &v) {
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << " ";
    os << *it;
  }
  return os;
}

// ../../../lib/template/initialize.cpp

struct yes_no : std::numpunct<char> {
  string_type do_truename() const { return "Yes"; }
  string_type do_falsename() const { return "No"; }
};

struct Initialize {
  Initialize() {
    std::locale loc(std::locale(), new yes_no);
    std::cout << std::boolalpha << std::setprecision(11) << std::fixed;
    std::cout.imbue(loc);
  }
};

const Initialize initialize;

// ../../../lib/template/macros.hpp

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

// ../../../lib/template/misc.hpp

template <typename T> T &chmin(T &a, const T &b) { return a = std::min(a, b); }

template <typename T> T &chmax(T &a, const T &b) { return a = std::max(a, b); }

// ../../../lib/template/range.hpp

class range {
  class index_type {
    int x;

  public:
    index_type(int x_) : x(x_) {}
    int operator*() const { return x; }
    bool operator!=(index_type &r) { return x < r.x; }
    void operator++() { ++x; }
  };
  index_type i, n;

public:
  range(int n_) : i(0), n(n_) {}
  range(int i_, int n_) : i(i_), n(n_) {}
  index_type &begin() { return i; }
  index_type &end() { return n; }
};

// ../../../lib/template/typedef.hpp

using ll = long long;
using ld = long double;

// ../../../lib/template/template.cpp

using namespace std;

// E.cpp

template<typename T>
vector<T> table(int n, T v) { return vector<T>(n, v); }

template <class... Args>
auto table(int n, Args... args) {
  auto val = table(args...);
  return vector<decltype(val)>(n, move(val));
}

ll mini, maxi;
ll a[100010], b[100010];
bool visited[100010];
vector<pair<int,ll>> g[100010];

void dfs(int v) {
  visited[v] = true;
  for (auto e: g[v]) {
    int w;
    ll sum;
    tie(w, sum) = e;
    ll new_a = -a[v];
    ll new_b = sum - b[v];
    if (visited[w]) {
      if (a[w] == new_a) {
        if (b[w] == new_b) continue;
        mini = 1e18; maxi = 0; continue;
      }
      else {
        if ((b[w] + new_b) & 1) {
          mini = 1e18; maxi = 0; continue;
        }
        else {
          if (a[w] == 1) {
            ll val = new_b - b[w];
            chmax(mini, val / 2);
            chmin(maxi, val / 2 + 1);
          }
          else {
            ll val = b[w] - new_b;
            chmax(mini, val / 2);
            chmin(maxi, val / 2 + 1);
          }
        }
      }
    }
    else {
      a[w] = new_a;
      b[w] = new_b;
      if (a[w] == 1) chmax(mini, -b[w] + 1);
      else chmin(maxi, b[w]);
      dfs(w);
    }
  }
}

void solve(ll n, ll m, vector<ll> u, vector<ll> v, vector<ll> s) {
  REP(i,m) {
    const int from = u[i] - 1, to = v[i] - 1;
    g[from].emplace_back(to, s[i]);
    g[to].emplace_back(from, s[i]);
  }
  a[0] = 1; b[0] = 0;
  mini = 1; maxi = 1e18;
  dfs(0);
  cout << max(0LL, maxi - mini) << endl;
}

int main() {
	ll n;
	ll m;
	scanf("%lld", &n);
	scanf("%lld", &m);
	vector<ll> s(m-1+1);
	vector<ll> u(m-1+1);
	vector<ll> v(m-1+1);
	for (int i = 0 ; i <= m-1 ; i++) {
	  scanf("%lld", &u[i]);
	  scanf("%lld", &v[i]);
	  scanf("%lld", &s[i]);
	}
	solve(n, m, u, v, s);
	return 0;
}
// ===== 2018/07/07 21:34:08 =====



Measured memory_bytes: 17280