#include <bits/stdc++.h>
using namespace std;
using LL = long long;
struct point {
  int to, cost;
};
vector<point> a[100000];
int N, M;
int dis[100000];
bool solve() {
  memset(dis, -1, sizeof(int));
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    for (point elm : a[now]) {
      if (dis[elm.to] == -1) {
        dis[elm.to] = elm.cost + dis[now];
      } else {
        if (dis[elm.to] != elm.cost + dis[now]) {
          return false;
        }
      }
    }
  }
}
int main(void) {
  cin >> N >> M;
  for (int i = 0; i < M; ++i) {
    int r, l, d;
    cin >> r >> l >> d;
    point s = {l, d};
    a[r].push_back(s);
    s = {r, -1 * d};
    a[l].push_back(s);
  }
  cout << solve();
  return 0;
}
