// Slower C++ solution:
/*Lucky_Glass*/

#include<cstdio>

#include<cstring>

#include<algorithm>

//#include<iostream>

#include<cmath>

#include<vector>

#include<queue>

using namespace std;

const int MAXN=1e5;

int n_pnt,n_edg;

struct NODE{

	int p,val;//val+p*x>0

	NODE(){}

	NODE(int a,int b){p=a;val=b;}

};

struct QUEUE{

	int u,p;//val+p*x>0

	long long val;

	QUEUE(){}

	QUEUE(int a,int b,long long c){u=a;p=b;val=c;}

};

vector<pair<int,int> > lnk[MAXN+5];

long long vis[2][MAXN+5];//vis[0][i]-x>0 && vis[1][i]+x>0 -> x<vis[0] && x>-vis[1]

inline int G(int p){return p==1;}

long long MIN=-1e15,MAX=1e15;

int main()

{

	fill(vis[0],vis[0]+MAXN+5,1e15);

	fill(vis[1],vis[1]+MAXN+5,1e15);

	scanf("%d%d",&n_pnt,&n_edg);

	for(int i=0,u,v,l;i<n_edg;i++)

		scanf("%d%d%d",&u,&v,&l),

		lnk[u].push_back(make_pair(v,l)),lnk[v].push_back(make_pair(u,l));

	queue<QUEUE> que;

	que.push(QUEUE(1,1,0));

	while(!que.empty())

	{

		QUEUE fro=que.front();que.pop();

		for(int i=0;i<lnk[fro.u].size();i++)

		{

			int v=lnk[fro.u][i].first,l=lnk[fro.u][i].second;

			long long val=l-fro.val;int p=fro.p==1? -1:1;

			if(vis[G(p)][v]!=1e15)

			{

				if(val!=vis[G(p)][v])

				{

					printf("%d\n",0);

					return 0;

				}

				else

					continue;

			}

			vis[G(p)][v]=val;

			if(G(p))

				MIN=max(MIN,max((long long)0,-vis[1][v]));

			else

				MAX=min(MAX,vis[0][v]);

			que.push(QUEUE(v,p,val));

		}

	}

	long long ans=-1;

	for(int i=1;i<=n_pnt;i++)

		if(vis[0][i]!=1e15 && vis[1][i]!=1e15)

		{

			int res=(vis[0][i]-vis[1][i])/2;

			if(res<0) {printf("0\n");return 0;}

			if(ans==-1) ans=res;

			if(ans!=res) {printf("0\n");return 0;}

		}

	if(ans!=-1)

	{

		if(MIN<ans && ans<MAX)

			printf("%d\n",1);

		else

			printf("%d\n",0);

		return 0;

	}

	if(MAX-MIN-1<0) printf("%d\n",0);

	else printf("%lld\n",MAX-MIN-1);

	return 0;

}

/*

2 1

1 2 3

*/


// Faster C++ solution for the same problem (measured speedup: 2.0304332290429494x):
#include<iostream>

#include<cstdio>

#include<cstring>

#include<climits>

#include<algorithm>

#include<cmath>

#include<queue>

using namespace std;

#define MAXN 100000

#define MAXM 100000

#define INF 1000000000000000

struct node

{

    int next,to;

    long long w;

}e[MAXM*2+5];

struct Qr

{

    int xr,dec;

    long long lim;

};

long long T;

int cnt,m,n;

int x,y,z,flag;

long long ri=INF,le=-INF;

long long vis[2][MAXN+5];

int b[MAXN+5];

void fpush(int u,int v,long long w)

{

    e[++cnt].next=b[u];

    e[cnt].w=w;

    e[cnt].to=v;

    b[u]=cnt;

}

int Ch(int x){if(x==-1)x=0;return x;}

void dfs(int xr,int dec,long long lim)

{

    if(flag)return ;

    for(int i=b[xr];i;i=e[i].next)

    {

        int xnext=e[i].to;

        long long nlim=e[i].w-lim;

        int FD=Ch(dec);

        if(vis[FD][xnext]!=INF)

        {

            if(vis[FD][xnext]!=nlim){flag=1;return ;}

            else continue;

        }

        vis[FD][xnext]=nlim;

        if(!FD)le=max(le,-vis[FD][xnext]);

        else ri=min(ri,vis[FD][xnext]);

        dfs(xnext,-dec,nlim);

    }

}

int main()

{

    fill(vis[0],vis[0]+MAXN+1,INF);

    fill(vis[1],vis[1]+MAXN+1,INF);

    scanf("%d%d",&n,&m);

    for(int i=1;i<=m;i++)

    {

        scanf("%d%d%d",&x,&y,&z);

        fpush(x,y,z);fpush(y,x,z);

    }

    dfs(1,1,0);

    if(flag){printf("0\n");return 0;}

    long long T=-1;//

    for(int i=1;i<=n;i++)

        if(vis[0][i]!=INF&&vis[1][i]!=INF)

        {

            long long NT=(vis[1][i]-vis[0][i])/2;

            if(NT<0){flag=1;break;}

            if(T==-1)T=NT;

            else if(T!=NT){flag=1;break;}

        }

    if(flag){printf("0\n");return 0;}

    if(T!=-1)

    {

        if(T>le&&T<ri)printf("1\n");

        else printf("0\n");

    }

    else printf("%lld",max((long long)0,ri-le-1));

}