using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Competitive
{
    internal class Solution
    {
        public int N, M;
        public Graph G;
        public void Run()
        {
            // Input
            {
                var line = Input.ReadIntArray();
                N = line[0];
                M = line[1];
            }
            G = new Graph(N);

            for (int i = 0; i < M; i++)
            {
                var line = Input.ReadIntArray();
                int u = line[0] - 1;
                int v = line[1] - 1;
                int s = line[2];
                var e1 = new Edge(u, v, s);
                var e2 = new Edge(v, u, s);
                e1.Reverse = e2;
                e2.Reverse = e1;
                G[u].Edges.Add(e1);
                G[v].Edges.Add(e2);
            }

            // Create Tree
            {
                var pq = new Queue<EdgeData>();
                pq.Enqueue(new EdgeData(1, new Edge(-1, 0, 1)));

                while (pq.Count() > 0)
                {
                    var d = pq.Dequeue();
                    var n = G[d.Edge.To];
                    if (n.Sign != 0) continue;

                    int from = d.Edge.From;
                    long fromValue = from == -1 ? 0 : G[from].Value;
                    n.Sign = d.Sign;
                    n.Value = d.Edge.Value - fromValue;
                    d.Edge.IsBranch = true;
                    if(d.Edge.Reverse != null)
                        d.Edge.Reverse.IsBranch = true;
                    foreach (var ee in n.Edges)
                    {
                        pq.Enqueue(new EdgeData(d.Sign * (-1), ee));
                    }
                }
            }

            // Branchでないもの
            List<Edge> consDiffSign = new List<Edge>();
            List<Edge> consSameSign = new List<Edge>();
            for (int i = 0; i < N; i++)
            {
                foreach (var e in G[i].Edges)
                    if(!e.IsBranch)
                    {
                        var from = G[e.From];
                        var to = G[e.To];
                        if (from.Sign == to.Sign) consSameSign.Add(e);
                        else consDiffSign.Add(e);
                    }
            }

            if (consDiffSign.Count != 0)
            {
                for (int c = 0; c < consDiffSign.Count; c++)
                {
                    var con = consDiffSign[c];
                    var from = G[con.From];
                    var to = G[con.To];
                    if (from.Value + to.Value != con.Value)
                    {
                        Console.WriteLine(0);
                        return;
                    }
                }
            }

            if (consSameSign.Count == 0)
            {
                // 制約が無い場合
                long plusMin = long.MaxValue;
                long minusMin = long.MaxValue;

                for (int i = 0; i < N; i++)
                {
                    if (G[i].Sign == 1) plusMin = Math.Min(plusMin, G[i].Value);
                    if (G[i].Sign == -1) minusMin = Math.Min(minusMin, G[i].Value);
                }

                long mn = 2 - Math.Min(plusMin, 1); // プラスノードに０以下がある場合、その分だけ足す必要がある
                long mx = minusMin; // マイナスノードが２なら、最大でも２まで

                Console.WriteLine(Math.Max(mx - mn + 1, 0));
            }
            else
            {
                long up;
                {
                    var con = consSameSign.First();
                    var fr = G[con.From];
                    var to = G[con.To];
                    var diff = (con.Value - (fr.Value + to.Value)) * fr.Sign;
                    if (diff < 0 || diff % 2 != 0)
                    {
                        Console.WriteLine(0);
                        return;
                    }

                    up = diff / 2;
                }

                for (int i = 0; i < N; i++)
                {
                    if (G[i].Value + up * G[i].Sign <= 0)
                    {
                        Console.WriteLine(0);
                        return;
                    }
                }

                for (int c = 0; c < consSameSign.Count; c++)
                {
                    var con = consSameSign[c];
                    var from = G[con.From];
                    var to = G[con.To];
                    if (from.Value + up * from.Sign + to.Value + up * to.Sign != con.Value)
                    {
                        Console.WriteLine(0);
                        return;
                    }
                }

                // Survived
                Console.WriteLine(1);
            }

        }
    }

    // libs ----------
    // 通常はIComparableのみが必要
    class EdgeData : IComparable<EdgeData>, IComparable
    {
        public int Sign;
        public Edge Edge;
        public EdgeData(int sign, Edge edge)
        {
            Sign = sign;
            Edge = edge;
        }

        public int CompareTo(EdgeData other)
        {
            return Sign.CompareTo(other.Sign);
        }

        public int CompareTo(object obj)
        {
            if (obj == null) return 1;
            if (GetType() != obj.GetType()) throw new ArgumentException();
            return CompareTo((EdgeData)obj);
        }
    }

    internal class Graph
    {
        public List<Node> Nodes;

        public Node this[int i]
        {
            set { Nodes[i] = value; }
            get { return Nodes[i]; }
        }

        public Graph(int N)
        {
            Nodes = new List<Node>();
            for (int i = 0; i < N; i++)
            {
                Nodes.Add(new Node(i));
            }
        }

    }

    internal class Node
    {
        public List<Edge> Edges;
        public int No;
        public int Sign;
        public long Value;

        public Node(int no)
        {
            this.No = no;
            Value = -1;
            Edges = new List<Edge>();
            Sign = 0;
        }
    }

    internal class Edge
    {
        public int From;
        public int To;

        public bool IsBranch;
        public long Value;
        public Edge Reverse;

        public Edge(int fr, int to, long value)
        {
            From = fr;
            To = to;
            Value = value;
            IsBranch = false;
        }

    }


    // common ----------

    internal static class Input
    {
        public static string ReadString()
        {
            string line = Console.ReadLine();
            return line;
        }

        public static int ReadInt()
        {
            string line = Console.ReadLine();
            return int.Parse(line);
        }

        public static int[] ReadIntArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(int.Parse).ToArray();            
        }

        public static long ReadLong()
        {
            string line = Console.ReadLine();
            return long.Parse(line);
        }

        public static long[] ReadLongArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(double.Parse).ToArray();
        }
    }
    
    internal class Program
    {
        public static void Main(string[] args)
        {
            var s = new Solution();
            s.Run();
        }
    }
}

Measured memory_bytes: 54024