#include <bits/stdc++.h>
using namespace std;

template <typename T>
class arr {
public:
  int n;
  T *a;
  void raise(int x) {
    T *b=a;
    a=new T[x];
    copy(b,b+n,a);
    delete b;
  }
  arr& operator << (T x) {
    if ((n&-n)==n) {
      raise(n<<1);
    }
    a[n++]=x;
    return *this;
  }
  void resize(int x) {
    if (n<x) {
      raise(1<<(32-__builtin_clz(x)));
    }
    n=x;
  }
  arr() {
    n=0;
    a=NULL;
  }
  arr(int x) {
    n=0;
    a=NULL;
    resize(x);
  }
  T& operator [] (int x) {
    return a[x];
  }
  int size() {
    return n;
  }
  T* begin() {
    return a;
  }
  T* end() {
    return a+n;
  }
};

const int N=1e5+5;

int n;
int m;

struct edge {
  int from;
  int to;
  int w;
};
arr<edge> edges;
arr<int> g[N];

void add_edge(int u,int v,int w) {
  int id=edges.size();
  edges<<(edge){u,v,w};
  g[u]<<id;
  g[v]<<id;
}

int len;
arr<int> cir;
arr<int> val;

bool dfs(int u,int fa) {
  static int vis[N];
  static int dep[N];
  if (vis[u]) {
    cir<<u;
    return 1;
  }
  vis[u]=1;
  for (auto id:g[u]) {
    auto &e=edges[id];
    int v=u^e.from^e.to;
    if (v!=fa) {
      if (vis[v]) {
	if ((dep[u]-dep[v]+1)&1) {
	  len=dep[u]-dep[v]+1;
	  cir<<v;
	  cir<<u;
	  val<<e.w;
	  return 1;
	}
      } else {
	dep[v]=dep[u]+1;
	if (dfs(v,u)) {
	  cir<<u;
	  val<<e.w;
	  return 1;
	}
      }
    }
  }
  return 0;
}


void bad() {
  printf("0\n");
  exit(0);
}

void judge(int u,int fa) {
  static int vis[N];
  static int dep[N][2];
  static int len[N];
  vis[u]=1;
  for (auto i:g[u]) {
    auto &e=edges[i];
    int v=u^e.from^e.to;
    if (!vis[v]) {
      dep[v][0]=dep[u][1]+e.w;
      dep[v][1]=dep[u][0]-e.w;
      len[v]=len[u]+1;
      judge(v,u);
    } else {
      if ((len[u]-len[v]+1)%2==0) {
	if (e.w!=dep[u][0]-dep[v][1]) {
	  bad();
	}
      }
    }
  }
}

int x[N];
int mnd[N];

int a[N];
int b[N];
int w[N];

void solve(int u,int fa) {
  static int vis[N];
  vis[u]=1;
  for (auto i:g[u]) {
    auto &e=edges[i];
    int v=u^e.from^e.to;
    if (!vis[v]) {
      x[v]=e.w-x[u];
      solve(v,u);
    }
  }
}

int f[N][2];

void dp(int u,int fa) {
  static int vis[N];
  int ch=0;
  f[u][0]=1;
  f[u][1]=1e9;
  vis[u]=1;
  for (auto i:g[u]) {
    auto &e=edges[i];
    int v=u^e.from^e.to;
    if (!vis[v]) {
      dp(v,u);
      f[u][0]=max(f[u][0],e.w-f[v][1]);
      f[u][1]=min(f[u][1],e.w-f[v][0]);
      ++ch;
    }
  }
  if (ch==0) {
    f[u][0]=1;
    f[u][1]=mnd[u]-1;
  }
}

int main() {
  scanf("%d %d",&n,&m);
  memset(mnd,0x3f3f,sizeof(mnd));
  for (int i=0;i<m;++i) {
    int u,v,_w;
    scanf("%d %d %d",&u,&v,&_w);
    --u;
    --v;
    add_edge(u,v,_w);
    mnd[u]=min(mnd[u],_w);
    mnd[v]=min(mnd[v],_w);
    a[i]=u;
    b[i]=v;
    w[i]=_w;
  }
  if (dfs(0,-1)) {
    if (len&1) {
      long long tot=0;
      memset(x,-1,sizeof(x));
      for (int i=0;i<len;++i) {
	tot+=(i&1)?-val[i]:val[i];
      }
      if (tot&1) {
	bad();
      }
      x[cir[0]]=tot/2;
      solve(cir[0],-1);
      
      for (int i=0;i<n;++i) {
	if (x[i]<=0) {
	  bad();
	}
      }
     for (int i=0;i<m;++i) {
	if (x[a[i]]+x[b[i]]!=w[i]) {
	  bad();
	}
      }
      printf("1\n");
      return 0;
    } else {
      judge(0,-1);
      dp(0,-1);
    }
  } else {
    judge(0,-1);
    dp(0,-1);
  }
  for (int i=0;i<n;++i) {
    if (f[i][0]>f[i][1]) {
      bad();
    }
  }
  if (f[0][1]-f[0][0]==0) {
    x[0]=f[0][1];
    solve(0,-1);
    for (int i=0;i<n;++i) {
      if (x[i]<=0) {
	bad();
      }
    }
    for (int i=0;i<m;++i) {
      if (x[a[i]]+x[b[i]]!=w[i]) {
	bad();
      }
    }
  }
  printf("%d\n",f[0][1]-f[0][0]+1);
  return 0;
}



Measured memory_bytes: 18996