import sys sys.setrecursionlimit(5*10**5) input = sys.stdin.readline from collections import defaultdict, deque, Counter from heapq import heappop, heappush from bisect import bisect_left, bisect_right from math import gcd dout = defaultdict(int) din = defaultdict(int) n,m = map(int,input().split()) for i in range(m): a,b = map(int,input().split()) dout[b-1]+=1 din[a-1]+=1 cnt1=0 cnt2=0 for i in range(n): if dout[i] == din[i]: continue elif dout[i] > din[i]: cnt1+= dout[i] - din[i] else: cnt2+= din[i] - dout[i] if cnt1 != cnt2: print(-1) elif cnt1+cnt2 == 0: print(0) else: print((cnt1+cnt2)//2-1)