import sys
n, m = map(int, sys.stdin.readline().split())
li = [set() for _ in range(m)]
for i in range(m):
  li[i].add(list(map(int, sys.stdin.readline().split())))
s = 0
for i in li:
  l = len(i)
  if l >= 2:
    print(-1)
    break
  else:
    s += int(*i)
else:
  print(s)