print(-1) exit() n, m = map(int,input().split()) din = [0] * n dout = [0] * n for i in range(m): u, v = map(int,input().split()) u -= 1 v -= 1 din[v] += 1 dout[u] += 1 tmp = [] for i in range(n): tmp.append(dout[i] - din[i]) tmp.sort() ans = 998244353 # euler graph ans = 0 for i in range(n): if tmp[i] > 0: ans += tmp[i] #ans = min(ans, tar) if ans == 0: print(0) exit() # jun euler graph ans -= 1 print(ans)