n, m = map(int,input().split())
a = []
b = []
for i in range(m):
  x, y = map(int,input().split())
  a.append(x)
  b.append(y)
ans = 0

def conflict_check(a, b):
  ini = 0
  for i in range(m):
    for j in range(m-i-1):
      jj = i+1+j
      if a[i] == a[jj]:
        if b[i] != b[jj]:
          ini -= 1
  return ini

if 1 in a: 
  if b[a.index(1)] == 0:
    ans -= 1
  else:
    ans += conflict_check(a, b)
else:
  ans += conflict_check(a, b)

if ans != -1:
  for i in range(n):
    if (i+1 in a) == True:
      ans += b[a.index(i+1)]*10**(2-i)

print(ans)