import sys def main(): input = sys.stdin.read().split() idx = 0 N = int(input[idx]) idx += 1 M = int(input[idx]) idx += 1 balance = [0] * (N + 1) # 1-based indexing for _ in range(M): u = int(input[idx]) idx += 1 v = int(input[idx]) idx += 1 balance[u] += 1 balance[v] -= 1 P = 0 N_count = 0 S_plus = 0 for i in range(1, N+1): b = balance[i] if b > 0: P += 1 S_plus += b elif b < 0: N_count += 1 # Check if already satisfies Eulerian trail condition if (P == 0 and N_count == 0) or (P == 1 and N_count == 1 and S_plus == 1): print(0) return # Check if P and N are in the correct relationship if not ((P == N_count) or (P == N_count + 1) or (N_count == P + 1)): print(-1) return # Calculate E E = max(P-1, N_count-1) if (S_plus - E) in (0, 1): print(E) else: print(-1) if __name__ == "__main__": main()