#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <climits>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

struct e
{
    int to;
    ll c;
};

vector<e> g[112345];

int dou[112345];

ll v[112345];

bool used[112345];

bool check2Graph(int p)
{
    bool res = false;
    for (auto i : g[p])
    {
        if (dou[i.to] == dou[p])
        {
            res |= true;
        }
        else
        {
            if (dou[i.to] == -1)
            {
                dou[i.to] = 1 - dou[p];
                res |= check2Graph(i.to);
            }
        }
    }

    return res;
}

pair<ll, int> dfs_nd(int p)
{
    used[p] = true;
    P res = {-1, -1};
    for (auto i : g[p])
    {
        if (used[i.to])
        {
            if (v[i.to] + v[p] != i.c && dou[i.to] == dou[p])
            {
                res = {i.c - (v[i.to] + v[p]), p};
            }
        }
        else
        {
            v[i.to] = i.c - v[p];
            pair<ll, int> d = dfs_nd(i.to);
            if (d.second != -1)
                res = d;
        }
    }

    return res;
}

bool dfs(int p)
{
    used[p] = true;
    bool res = true;
    for (auto i : g[p])
    {
        if (!used[i.to])
        {
            v[i.to] = i.c - v[p];
            if (!dfs(i.to))
                res = false;
        }
        else
        {
            if (v[i.to] + v[p] != i.c)
                res = false;
        }
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m;

    cin >> n >> m;
    REP(i, m)
    {
        int u, v, s;
        cin >> u >> v >> s;

        g[u].push_back({v, s});
        g[v].push_back({u, s});
    }

    memset(dou, -1, sizeof(dou));
    dou[1] = 0;

    ll res = 0;

    if (check2Graph(1))
    {
        //Not 2

        fill(used, used + n + 1, false);
        memset(v, -1, sizeof(v));

        v[1] = 1;

        if (dfs(1))
        {
            bool check = true;
            for (int i = 1; i <= n; i++)
                if (v[i] < 1)
                    check = false;
            if (check)
                res = 1;
            else
                res = 0;
        }
        else
        {
            fill(used, used + n + 1, false);
            memset(v, -1, sizeof(v));

            P d = dfs_nd(1);

            if (d.first % 2)
            {
                res = 0;
            }
            else
            {
                ll tmp = v[d.second] + d.first / 2;
                fill(used, used + n + 1, false);
                memset(v, -1, sizeof(v));
                v[d.second] = tmp;

                if (dfs(d.second))
                {
                    bool check = true;
                    for (int i = 1; i <= n; i++)
                        if (v[i] < 1)
                            check = false;
                    if (check)
                        res = 1;
                    else
                        res = 0;
                }
                else
                {
                    res = 0;
                }
            }
        }
    }
    else
    {
        // 2

        fill(used, used + n + 1, false);
        memset(v, -1, sizeof(v));

        v[1] = 1;

        if (!dfs(1))
        {
            res = 0;
        }
        else
        {
            ll st = 1; //v[1]の開始地点
            ll gl = 0; //v[1]の終了地点

            ll mis = LLONG_MAX;
            ll mig = LLONG_MAX;
            for (int i = 1; i <= n; i++)
                if (dou[i] == 0)
                    mis = min(mis, v[i]);

            if (mis < 1)
                st = 2 - mis;

            for (int i = 1; i <= n; i++)
                if (dou[i] == 1)
                    mig = min(mig, v[i]);

            gl = mig;

            res = max(0ll, gl - st + 1);
        }
    }

    cout << res << endl;

    return 0;
}

Measured memory_bytes: 14208