import sys input = sys.stdin.readline def main(): N, M = map(int, input().split()) l = [list(map(int, input().split())) for l in range(M)] lout = [0] * (N+1) lin = [0] * (N+1) lans = [0] * (N+1) for i in range(M): lout[l[i][0]] += 1 lin[l[i][1]] += 1 for i in range(1, N + 1): lans[i] = lout[i] - lin[i] lpos = sum([i for i in lans if i > 0]) lneg = sum([i for i in lans if i < 0]) if lpos == -lneg: print(lpos - 1) else: print(-1) if __name__ == '__main__': main()