using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace AtCoder
{
    class Program
    {
        private static string Read() { return Console.ReadLine(); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }

        class Edge
        {
            public readonly int v;
            public readonly long s;
            public Edge(int v, long s)
            {
                this.v = v;
                this.s = s;
            }
        }
        static long Solve(int n, List<Edge>[] nodes)
        {
            var tuples = new Tuple<long, int>[n];
            var q = new Queue<int>();
            tuples[0] = new Tuple<long, int>(0, 1);
            q.Enqueue(0);
            long min = long.MinValue;
            long max = long.MaxValue;
            while (q.Count > 0)
            {
                int u = q.Dequeue();
                foreach (var edge in nodes[u])
                {
                    int v = edge.v;
                    long s = edge.s;
                    var L = tuples[v];
                    var R = new Tuple<long, int>(s - tuples[u].Item1, -tuples[u].Item2);
                    if (tuples[v] == null)
                    {
                        tuples[v] = R;
                        q.Enqueue(v);
                    }
                    else if (L.Item2 != R.Item2)
                    {
                        if ((L.Item1 + R.Item1) % 2 == 0)
                        {
                            long x = -(L.Item1 * L.Item2 + R.Item1 * R.Item2) / 2;
                            min = Math.Max(x, min);
                            max = Math.Min(x, max);
                        }
                        else
                        {
                            return 0;
                        }
                    }
                    else if (L.Item1 != R.Item1)
                    {
                        return 0;
                    }
                }
            }
            foreach (var tuple in tuples)
            {
                switch (tuple.Item2)
                {
                case -1: max = Math.Min( tuple.Item1 - 1, max); break;
                case  1: min = Math.Max(-tuple.Item1 + 1, min); break;
                }
            }
            return Math.Max(max - min + 1, 0);
        }
        static void Main(string[] args)
        {
            int[] V1 = ReadInts();
            int n = V1[0];
            int m = V1[1];
            var nodes = new List<Edge>[n];
            for (int i = 0; i < n; ++i) { nodes[i] = new List<Edge>(); }
            for (int i = 0; i < m; ++i)
            {
                int[] V2 = ReadInts();
                int u = V2[0] - 1;
                int v = V2[1] - 1;
                int s = V2[2];
                nodes[u].Add(new Edge(v, s));
                nodes[v].Add(new Edge(u, s));
            }
            Console.WriteLine(Solve(n, nodes));
        }
    }
}


Measured memory_bytes: 41532