#include<iostream>
#include<sstream>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cassert>
#include<cctype>
#include<ctime>
#include<cstdlib>
#include<map>
#include<set>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<string>
#include<algorithm>
#include<utility>
using namespace std;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define il inline
#define rg register
#define lch(x) ((x)<<1)
#define rch(x) ((x)<<1^1)
#define rep(i,st,ed) for(rg int i=(st);i<=(ed);++i)
#define per(i,ed,st) for(rg int i=(ed);i>=(st);--i)
#define rep0(i,n) for(rg int i=0;i<(n);++i)
#define per0(i,n) for(rg int i=(n)-1;i>=0;--i)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef double dbl;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
template<typename T> il T qmin(const T &x,const T &y){return x<y?x:y;}
template<typename T> il T qmax(const T &x,const T &y){return x>y?x:y;}
template<typename T> il void getmin(T &x,const T &y){if(y<x) x=y;}
template<typename T> il void getmax(T &x,const T &y){if(y>x) x=y;}
il void fileio(const string &s){
	freopen((s+".in").c_str(),"r",stdin);
	freopen((s+".out").c_str(),"w",stdout);
}

const int inf=(int)1e9+7;
const ll linf=(ll)1e18+7;
const int N=1e5+7,M=N<<1;

int v[M],nxt[M];
int head[N],dep[N];
int n,m;

il void addedge(int p,int x,int y){
	v[p]=y,nxt[p]=head[x],head[x]=p;
}

namespace task1{
	ll ans=0;
	
	il int dfs(int u,int p){
		dep[u]=dep[p]+1;
		int rest=(dep[u]&1?1:-1);
		for(int e=head[u];e;e=nxt[e]){
			if(v[e]==p) continue;
			rest+=dfs(v[e],u);
		}
		ans+=abs(rest);
		return rest;
	}
	
	il int solve(){
		if(dfs(1,0)) return puts("-1"),0;
		printf("%lld\n",ans);
		return 0;
	}
}

namespace task2{
	bool vis[N],used[M];
	int stk[N],top;
	ll val[N],ans=0;
	int len;
	vector<int> node;
	
	il bool init(int u){
		vis[u]=true;
		stk[++top]=u;
		for(int e=head[u];e;e=nxt[e]){
			if(used[e>>1]) continue;
			used[e>>1]=true;
			if(vis[v[e]]){
				while(stk[top]!=v[e]) node.pb(stk[top--]);
				node.pb(v[e]);
				return true;
			}else{
				if(init(v[e])) return true;
			}
		}
		--top;
		return false;
	}
	
	il int dfs(int u,int c){
		vis[u]=true;
		int rest=(c?1:-1);
		for(int e=head[u];e;e=nxt[e]){
			if(vis[v[e]]) continue;
			rest+=dfs(v[e],c^1);
		}
		ans+=abs(rest);
		return rest;
	}
	
	il ll calc(ll sum){
		ll res=0;
		rep0(i,len) res+=abs(sum+=val[i]);
		return res;
	}
	
	
	il int solve(){
		init(1);
		len=node.size();
		memset(vis+1,0,sizeof(bool)*n);
		rep0(i,len) vis[node[i]]=true;
		ll tot=0;
		rep0(i,len){
			val[i]=dfs(node[i],i&1);
			ans-=abs(val[i]);
			tot+=val[i];
		}
	//	cerr<<len<<" "<<tot<<" "<<ans<<endl;
		if(len&1){
			//same
			tot>>=1;
			val[0]-=tot;
			val[len-1]-=tot;
			printf("%lld\n",ans+calc(0)+abs(tot));
		}else{
			//different
			if(tot) return puts("-1"),0;
			int ul=-n/2,ur=n/2;
			while(ur-ul>3){
				int m1=(2*ul+ur)/3,m2=(ul+2*ur)/3;
				if(calc(m1)<calc(m2)) ur=m2-1;
				else ul=m1+1;
			}
			ll res=calc(ul);
			rep(i,ul+1,ur) getmin(res,calc(i));
			printf("%lld\n",ans+res);
		}
		return 0;
	}
}

int main(){
//	fileio("test");
	scanf("%d%d",&n,&m);
	if(n&1) return puts("-1"),0;
	rep(i,1,m){
		int x,y;
		scanf("%d%d",&x,&y);
		addedge(lch(i),x,y);
		addedge(rch(i),y,x);
	}
	if(m==n-1) task1::solve();
	else task2::solve();
	return 0;
}
 		

Measured memory_bytes: 5120