#include <bits/stdc++.h>
using namespace std;
#define ri register int
typedef long long ll;
#define rll register ll
namespace io {
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	// getchar
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	// print the remaining part
	inline void flush () {
		fwrite (obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	// putchar
	inline void putc (char x) {
		*oS ++ = x;
		if (oS == oT) flush ();
	}
	// input a signed integer
	template <class I>
	inline void gi (I &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = (x << 1) + (x << 3) + (c & 15); x *= f;
	}
	// input a signed integer
	template <class I>
	inline void read (I &x, int p) {
		for (c = gc(); c < '0' || c > '9'; c = gc()) ;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = ( (x << 1) + (x << 3) + (c & 15) ) % p; 
	}
	// print a signed integer
	template <class I>
	inline void print (I x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
	}
	// no need to call flush at the end manually!
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
} 
using io :: gi;
using io :: read;
using io :: putc;
using io :: print;
const int N=1e5+5;
int tot,head[N],nxt[N<<1],adj[N<<1],col[N],a,b,c,id,f[N],g[N],tag[N],v[N];
inline void addedge(ri x,ri y){
	nxt[++tot]=head[x];
	adj[head[x]=tot]=y;
}
inline void dfs(ri x,ri fa){
	for(ri y,i=head[x];i;i=nxt[i])
		if((i^1)!=fa){
			if(!col[y=adj[i]])
				col[y]=-col[x],
				dfs(y,i);
			else
				a=x,b=y,c=col[x]==col[y],id=i;
		}
}
inline void dp(ri x,ri fa){
	f[x]=col[x];
	g[x]=tag[x];
	for(ri y,i=head[x];i;i=nxt[i])
		if((i^1)!=fa&&i!=id&&(i^1)!=id)
			dp(y=adj[i],i),
			f[x]+=f[y],
			g[x]+=g[y];
}
int main(){
	ri t,n,m,i,x,y,ans,cnt;
//	gi(t);
	t=1;
	while(t--){
		gi(n);gi(m);
		tot=1;
		for(i=1;i<=n;++i) head[i]=0;
		for(i=1;i<=m;++i) gi(x),gi(y),addedge(x,y),addedge(y,x);
		for(i=1;i<=n;++i) col[i]=0;
		col[1]=1;
		id=0;
		dfs(1,0);
		ans=0;
	//	printf("c = %d\n",c);
		if(m==n-1){
			dp(1,0);
			if(f[1]){
				puts("-1");
				continue;
			}
			for(i=1;i<=n;++i) ans+=abs(f[i]);
			printf("%d\n",ans);
		}
		else if(c){ // 奇环
			dp(1,0);
			if(f[1]%2){
				puts("-1");
				continue;
			}
		//	printf("check %d\n",f[1]);
			x=f[1]/2;
			ans=abs(x);
			col[a]-=x;
			col[b]-=x;
			dp(1,0);
			for(i=1;i<=n;++i) ans+=abs(f[i]);
			printf("%d\n",ans);
		}
		else{ // 偶环
			if(f[1]%2){ // +x -x 奇偶性不变
				puts("-1");
				continue;
			}
			for(i=1;i<=n;++i) tag[i]=0;
			tag[a]=1;
			tag[b]=-1;
			dp(1,0);
			// abs(x) = abs(0 - x)
			v[cnt=1]=0;
			for(i=1;i<=n;++i)
				if(!g[i]) ans+=abs(f[i]);
				else // abs(f[i] + g[i] * x)
					if(g[i]==1) // abs(-f[i] - x)
						v[++cnt]=-f[i];
					else // abs(f[i] - x) 
						v[++cnt]=f[i];
			sort(v+1,v+1+cnt);
			if(cnt&1)
				x=v[cnt/2+1];
			else
				x=v[cnt/2];
			ans+=abs(x);
			for(i=1;i<=n;++i)
				if(g[i])
					ans+=abs(f[i]+g[i]*x);
			printf("%d\n",ans);
		}
	}
    return 0;
}