#include<bits/stdc++.h> 
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
typedef long long ll;
using namespace std;
inline int read(){
	int n=0,f=1;char c;
	for(c=getchar();(c<'0'||c>'9')&&c!='-';c=getchar());
	if (c=='-') c=getchar(),f=-1;
	for(;c>='0'&&c<='9';c=getchar()) n=n*10+c-48;
	return n*f;
}
const int maxn=1e5+5;
int i,j,n,m,s[maxn],ans,t;
int k[maxn],g[maxn<<1],ne[maxn<<1],num,b;
int u,v,f[maxn],p[maxn];
int a[maxn],cnt;
bool bz[maxn],d[maxn];
void add(int x,int y){
	ne[++num]=k[x],k[x]=num,g[num]=y;
}
void dfs(int x,int y){
	int i;
	bz[x]=1,f[x]=y,d[x]=d[y]^1;
	s[x]=d[x]?1:-1;
	if (x==u||x==v) s[x]+=t;
	for(i=k[x];i;i=ne[i]){
		int go=g[i];
		if (go!=y){
			if (!bz[go]) dfs(go,x),s[x]+=s[go];else
			b=d[x]==d[go],u=x,v=go;
		}
	}
}
int main(){
	n=read(),m=read();
	fo(i,1,m){
		int  x=read(),y=read();
		add(x,y),add(y,x);
	}
	dfs(1,0);
	if ((s[1]&&!b)||(s[1]&1)) {
		puts("-1");return 0;
	}
	if (n==m-1){
		fo(i,1,n) ans+=abs(s[i]);
		printf("%d\n",ans);
		return 0;
	}
	memset(bz,0,sizeof(bz));
	if (b){
		t=-(s[1]/2);
		if (t){
			for(;u;u=f[u]) s[u]+=t;
			for(;v;v=f[v]) s[v]+=t;
		}
		ans=abs(t);
		fo(i,1,n) ans+=abs(s[i]);
		printf("%d\n",ans);
		return 0;
	}
	for(;u;u=f[u]) p[u]++;
	for(;v;v=f[v]) p[v]--;
	a[cnt=1]=0;
	fo(i,1,n) if (!p[i]) ans+=abs(s[i]);else
	a[++cnt]=(p[i]==1)?-s[i]:s[i];
	int mid=(cnt+1)/2;
	nth_element(a+1,a+mid,a+1+cnt);
	fo(i,1,cnt) ans+=abs(a[mid]-a[i]);
	printf("%d\n",ans);
}