local mmi, mma = math.min, math.max
local n, m = io.read("*n", "*n")
local edge = {}
local v = {}
local len = {}
for i = 1, n do
  edge[i] = {}
  v[i] = false
  len[i] = false
end
v[1] = 0
len[1] = 0
local evmin = 0
local oddmin = false
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  edge[a][b] = c
  edge[b][a] = c
  if a == 1 or b == 1 then
    oddmin = c
  end
end
local rem_edge = {}
local tasks = {1}
for i = 1, n do
  local src = tasks[i]
  for dst, sum in pairs(edge[src]) do
    if not v[dst] then
      edge[dst][src] = nil
      v[dst] = sum - v[src]
      len[dst] = 1 - len[src]
      if len[dst] == 1 then
        oddmin = mmi(oddmin, v[dst])
      else
        evmin = mmi(evmin, v[dst])
      end
      table.insert(tasks, dst)
    else
      table.insert(rem_edge, {src, dst, sum})
    end
  end
end
if #rem_edge == 0 then
  local diff = evmin - 1
  oddmin = oddmin + diff
  print(math.max(0, oddmin))
else
  local valid = true
  local offset = false
  for i = 1, #rem_edge do
    local p1, p2, sum = rem_edge[i][1], rem_edge[i][2], rem_edge[i][3]
    if len[p1] + len[p2] == 1 then
      if v[p1] + v[p2] ~= sum then
        valid = false break
      end
    else
      if len[p1] + len[p2] == 0 then
        if not offset then
          offset = sum - v[p1] - v[p2]
          if offset % 2 == 1 then valid = false break end
        else
          if v[p1] + v[p2] + offset * 2 ~= sum then
            valid = false break
          end
        end
      else
        if not offset then
          offset = v[p1] + v[p2] - sum
          if offset % 2 == 1 then valid = false break end
        else
          if v[p1] + v[p2] - offset * 2 ~= sum then
            valid = false break
          end
        end
      end
    end
    if offset then
      if evmin + offset < 1 then valid = false end
      if oddmin - offset < 1 then valid = false end
    end
  end
  print(valid and 1 or 0)
end
