#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int,int>
#define mk make_pair
#define pb push_back
#define eb emplace_back
#define ins insert
#define fi first
#define se second
#define Rep(x,y,z) for(int x=y;x<=z;x++)
#define Red(x,y,z) for(int x=y;x>=z;x--)
using namespace std;
const int MAXN=1e5+5;
char buf[1<<12],*pp1=buf,*pp2=buf,nc;int ny;
inline char gc() {return pp1==pp2&&(pp2=(pp1=buf)+fread(buf,1,1<<12,stdin),pp1==pp2)?EOF:*pp1++;}
//inline char gc(){return getchar();}
inline int read(){
	int x=0;for(ny=1;nc=gc(),(nc<48||nc>57)&&nc!=EOF;)if(nc==45)ny=-1;if(nc<0)return nc;
	for(x=nc-48;nc=gc(),47<nc&&nc<58&&nc!=EOF;x=(x<<3)+(x<<1)+(nc^48));return x*ny;
}
int n,m,f[MAXN],fa[MAXN],a[MAXN],rt=1,b[MAXN],re,tot;vector<int>G[MAXN];
void dfs(int x,int f,int d){fa[x]=f,a[x]=d&1?1:-1;for(auto y:G[x])if(y!=f)dfs(y,x,d+1),a[x]+=a[y];}
int gf(int x){return f[x]==x?x:f[x]=gf(f[x]);}
inline bool Merge(int x,int y){return gf(x)==gf(y)?0:f[gf(x)]=gf(y);}
int main(){
//	freopen("std.in","r",stdin);
//	freopen("std.out","w",stdout);
	n=read(),m=read();
	Rep(i,1,n)f[i]=i;
	Rep(i,1,m){int x=read(),y=read();Merge(x,y)?G[x].pb(y),G[y].pb(x),0:(rt=x,re=y);}
	dfs(rt,0,0);
	if(n==m){
		for(int t=re;t;t=fa[t])b[++tot]=a[t];
		if(tot&1){
			if(a[rt]&1)return puts("-1"),0;
			for(int t=re;t;t=fa[t])a[t]-=a[rt]>>1;
		}else{
			if(a[rt])return puts("-1"),0;
			sort(b+1,b+tot+1);
			for(int t=re;t;t=fa[t])a[t]-=b[tot>>1];
		}
	}else if(a[rt])return puts("-1"),0;
	int ans=0;Rep(i,1,n)ans+=abs(a[i]);cout<<ans<<"\n";
	return 0;
}


