data class Edge(val from: Int, val to: Int, val weight: Long)

fun oddCycle(graph: List<List<Edge>>): List<Int>? {
  val n = graph.size
  val colors = IntArray(n, { -1 })
  val prevs = IntArray(n, { -1 })
  var ans: List<Int>? = null

  fun dfs(v: Int, color: Int) {
    if (ans != null) {
      return
    }

    colors[v] = color
    for (e in graph[v]) {
      val u = e.to
      if (colors[u] == -1) {
        prevs[u] = v
        dfs(u, 1 - color)
        if (ans != null) {
          return
        }
      } else if (colors[u] == color) {
        // Found cycle
        val cycle = mutableListOf<Int>()
        var w = v
        // println("v: $v u: $u")
        while (true) {
          // println("w: $w")
          cycle.add(w)
          if (w == u) {
            break
          }
          w = prevs[w]
        }
        ans = cycle.toList()
        return
      }
    }
  }

  dfs(0, 0)

  return ans
}

fun getEdgeWeight(graph: List<List<Edge>>, v1: Int, v2: Int): Long {
  for (e in graph[v1]) {
    if (e.to == v2) {
      return e.weight
    }
  }

  throw RuntimeException()
}

fun main(args: Array<String>) {
  val (n, m) = readLine()!!.split(" ").map { it.toInt() }
  val mutableGraph = Array(n, { mutableListOf<Edge>() })
  repeat(m) {
    val (v1Raw, v2Raw, s) = readLine()!!.split(" ").map { it.toInt() }
    val v1 = v1Raw - 1
    val v2 = v2Raw - 1
    mutableGraph[v1].add(Edge(v1, v2, s.toLong()))
    mutableGraph[v2].add(Edge(v2, v1, s.toLong()))
  }
  val graph = mutableGraph.map { it.toList() }
  val maybeOddCycle = oddCycle(graph)
  if (maybeOddCycle != null) {
    val oddCycle = maybeOddCycle!!
    val weights = mutableListOf<Long>()
    for (i in 0 until oddCycle.size - 1) {
      weights.add(getEdgeWeight(graph, oddCycle[i], oddCycle[i + 1]))
    }
    weights.add(getEdgeWeight(graph, oddCycle[oddCycle.size - 1], oddCycle[0]))
    val weightTotal = weights.sum()
    if (weightTotal % 2 == 1L) {
      println(0)
      return
    }
    val w0 = weightTotal / 2 - weights.filterIndexed { index, value -> index % 2 == 1 }.sum()

    val values = LongArray(n, { Long.MIN_VALUE })
    fun dfs(v: Int, w: Long): Boolean {
      values[v] = w
      for (e in graph[v]) {
        if (values[e.to] == Long.MIN_VALUE) {
          if (!dfs(e.to, e.weight - values[e.from])) {
            return false
          }
        } else {
          if (values[e.to] + values[e.from] != e.weight) {
            return false
          }
        }
      }
      return true
    }
    if (dfs(oddCycle[0], w0)) {
      if (values.all { it >= 1 }) {
        println(1)
        return
      } else {
        println(0)
        return
      }
    } else {
      println(0)
      return
    }
  } else {
    val values0 = LongArray(n, { Long.MIN_VALUE })
    val values1 = LongArray(n, { Long.MIN_VALUE })
    fun dfs2(values: LongArray, v: Int, w: Long): Boolean {
      values[v] = w
      for (e in graph[v]) {
        if (values[e.to] == Long.MIN_VALUE) {
          if (!dfs2(values, e.to, e.weight - values[e.from])) {
            return false
          }
        } else {
          if (values[e.to] + values[e.from] != e.weight) {
            return false
          }
        }
      }
      return true
    }
    if (!dfs2(values0, 0, 0) || !dfs2(values1, 0, 1)) {
      println(0)
      return
    }
    var minV0 = 1L
    var maxV0 = Long.MAX_VALUE
    for (i in 0 until n) {
      if (values0[i] < values1[i]) {
        minV0 = Math.max(minV0, 1 - values0[i])
      } else if (values0[i] > values1[i]) {
        maxV0 = Math.min(maxV0, values0[i] - 1)
      }
    }

    println(if (maxV0 >= minV0) maxV0 - minV0 + 1 else 0)
  }
}
