#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>

#define N 101000
#define ll long long
#define RG register

using namespace std;

inline int read(){
    RG int x=0,t=1;RG char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    if(ch=='-')t=-1,ch=getchar();
    while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
    return x*t;
}

int n,m,top,first[N],f[N],deg[N]; ll ans;
struct mona { int nxt,en; } s[N<<1];
inline void Insert(int x,int y) { s[++top]=(mona) { first[x],y },first[x]=top; }
inline void Dfs(int k,int fa){
	f[k]=1;
	for(RG int i=first[k];i;i=s[i].nxt){
		int en=s[i].en; if(en==fa) continue ;
		Dfs(en,k),f[k]-=f[en],ans+=abs(f[en]);
	}
}
inline void Work1(){
	for(RG int i=1;i<=m;++i){
		int x=read(),y=read(); ++deg[x],++deg[y];
		Insert(x,y),Insert(y,x);
	}   Dfs(1,0); if(f[1]) { puts("-1"); return ; }
	printf("%lld\n",ans);
}

#define pb push_back
int vis[N],dfn[N],low[N],cnt,L[N],R[N]; vector<int> rt;
inline void Dfs1(int k,int fa){
	dfn[k]=low[k]=++cnt;
	for(RG int i=first[k];i;i=s[i].nxt){
		int en=s[i].en; if(en==fa) continue ;
		if(dfn[en]) { if(!vis[en]) low[k]=min(low[k],dfn[en]),rt.pb(en),vis[en]=1; }
		else Dfs1(en,k),low[k]=min(low[k],low[en]);
	}	if(low[k]<dfn[k]) rt.pb(k),vis[k]=1; return ;
}
inline void Dfs2(int k,int fa){
	f[k]=1;
	for(RG int i=first[k];i;i=s[i].nxt){
		int en=s[i].en; if(vis[en]||en==fa) continue ;
		Dfs2(en,k),f[k]-=f[en],ans+=abs(f[en]);
	}
}

struct Heap_g{
	priority_queue< int,vector<int>,greater<int> > A,B; ll sum;
	inline void push(int x) { A.push(x),sum+=x; }
	inline void del(int x) { B.push(x),sum-=x; while(A.size()&&B.size()&&A.top()==B.top()) A.pop(),B.pop(); }
	inline void pop() { while(A.size()&&B.size()&&A.top()==B.top()) A.pop(),B.pop(); sum-=A.top(),A.pop(); }
	inline int top() { while(A.size()&&B.size()&&A.top()==B.top()) A.pop(),B.pop(); return A.top(); }
	inline int size() { return A.size()-B.size(); }
} ;
struct Heap_l{
	priority_queue< int,vector<int>,less<int> > A,B; ll sum;
	inline void push(int x) { A.push(x),sum+=x; }
	inline void del(int x) { B.push(x),sum-=x; while(A.size()&&B.size()&&A.top()==B.top()) A.pop(),B.pop(); }
	inline void pop() { while(A.size()&&B.size()&&A.top()==B.top()) A.pop(),B.pop(); sum-=A.top(),A.pop(); }
	inline int top() { while(A.size()&&B.size()&&A.top()==B.top()) A.pop(),B.pop(); return A.top(); }
	inline int size() { return A.size()-B.size(); }
} ;
ll res,op=1,cp,len;
struct T_Heap{
	Heap_l A; Heap_g B;
	inline void init() { A.sum=B.sum=0; }
	inline void get(){
		while(A.size()<B.size()) A.push(B.top()),B.pop();
		while(A.size()>B.size()+1) B.push(A.top()),A.pop();
	}
	inline void push(int x) { if(!A.size()||x<=A.top()) A.push(x); else B.push(x); get(); }
	inline void del(int x) { if(x<=A.top()) A.del(x); else B.del(x); get(); }
	inline ll sum() { return B.sum-A.sum; }
} Q;

inline void Work2(){
	for(RG int i=1;i<=m;++i){
		int x=read(),y=read(); ++deg[x],++deg[y];
		Insert(x,y),Insert(y,x);
	}   Dfs1(1,0),len=rt.size();
	for(RG int i=0;i<len;++i) Dfs2(rt[i],0);
	if(len&1){
		int x=0; for(RG int i=0;i<len;++i) x+=(i&1?-1:1)*f[rt[i]];
		if(x&1) { puts("-1"); return ; } x>>=1;
		ans+=abs(x); for(RG int i=0;i<len-1;++i) x=f[rt[i]]-x,ans+=abs(x);
		printf("%lld\n",ans);
	}   else{
		int A=0,B=0; cp=len;
		for(RG int i=0;i<len;++i) (i&1?A:B)+=f[rt[i]];
		if(A!=B) { puts("-1"); return ; }
		for(RG int i=0;i<len;++i) rt.pb(rt[i]); len<<=1;
		Q.init(),Q.push(0); ll Min=1e18;
		for(RG int i=len;i>=3;--i){
			R[i]=R[i+1]+f[rt[i-1]]*(i&1?-1:1),Q.push(R[i]);
			if(len+1-i+1>=cp){
				Min=min(Min,Q.sum());
				Q.del(R[i+cp-1]),res=R[i+cp-2],op=-op;
			}
		}   printf("%lld\n",Min+ans);
	}
}

int main(){
	n=read(),m=read();
	if(m==n-1) Work1(); else Work2();
}


Measured memory_bytes: 9076