#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}

typedef long long ll;
int N,M;
vector<vector<int>> G;

ll anst;
int dfst(int v,int p){
	int t=1;
	for(int u:G[v]) if(u!=p){
		t-=dfst(u,v);
	}
	anst+=abs(t);
	return t;
}
ll solveTree(){
	if(dfst(0,-1)!=0){
		return -1;
	}
	return anst;
}


bool dfsc(int v, int p, const vector<vector<int>>& G, vector<int>& vis, vector<int>& cyc, bool& done){
	if(vis[v]==1){
		cyc.pb(v);
		return 1;
	}
	if(vis[v]==2) return 0;

	vis[v] = 1;
	for(int u:G[v]) if(u!=p){
		if(dfsc(u,v,G,vis,cyc,done)){
			if(v==cyc[0]) done=1;
			if(!done) cyc.pb(v);
			return 1;
		}
	}
	vis[v] = 2;
	return 0;
}
vector<int> getcycle(const vector<vector<int>>& G){
	int N = G.size();
	vector<int> vis(N,0);	//0:yet 1:now 2:done
	bool done = 0;
	vector<int> cyc;
	rep(i,N) if(vis[i]==0){
		if(dfsc(i,-1,G,vis,cyc,done)){
			reverse(all(cyc));
			return cyc;
		}
	}
	return {};
}

struct BIT{
	using D = ll;
	int N;
	vector<D> bit;
	BIT(int n){		//nは区間幅!!!
		N=n;
		bit.assign(n+1,0);
	}
	BIT(){}
	void init(int n){
		N=n;
		bit.assign(n+1,0);
	}
	D sum(int i){		//i個の和a[0]+..a[i-1]
		D s=0;
		while(i>0){
			s+=bit[i];
			i-=(i&-i);
		}
		return s;
	}
	D val(int i){	//return a[i]	(0-ind)
		return sum(i+1)-sum(i);
	}
	void add(int i,D x){	//a[i]+=x (0-indexed)
		i++;
		while(i<=N){
			bit[i]+=x;
			i+=(i&-i);
		}
	}
	void assign(int i,D x){	//a[i]=x
		add(i,x-val(i));
	}
};

ll ans;
int dfs(int v,int p1,int p2=-1){
	int t=1;
	for(int u:G[v]) if(u!=p1&&u!=p2){
		t-=dfs(u,v);
	}
	if(p2==-1) ans+=abs(t);
	return t;
}

ll solveNamori(){
	vector<int> cycle = getcycle(G);
	int K = cycle.size();
	vector<ll> dif(K);
	ll difsum = 0;
//	show(cycle);
	rep(i,K){
		int v = cycle[i];
		int p1 = cycle[(i-1+K)%K];
		int p2 = cycle[(i+1)%K];
		ll tmp = dfs(v,p1,p2);
		if(i%2) tmp = -tmp;
		dif[i] = tmp;
		difsum += dif[i];
	}
//	show(ans);
//	show(dif);
	if(K%2==0 && difsum!=0) return -1;
	if(K%2==1){
		if(difsum%2!=0) return -1;
		ll change = difsum/2;
		ans += abs(change);
		dif[0] -= change;
		dif[K-1] -= change;
	}

//	show(dif);

	int V = 2*N+100;

	BIT bitn(V),bits(V);
	int off = N+10;

	int sum = 0;
	rep(i,K){
		sum += dif[i];
		bitn.add(sum+off,1);
		bits.add(sum+off,sum+off);
	}
	ll res = 1e18;
	rep(i,K){
//		show(i);
		ll lnum = bitn.sum(off), rnum = bitn.sum(V)-lnum;
		ll lsum = bits.sum(off), rsum = bits.sum(V)-lsum;
		ll tmp = (lnum*off - lsum) + (rsum - rnum*off);
		chmin(res,tmp);

		bitn.add(dif[i]+off,-1);
		bits.add(dif[i]+off,-(dif[i]+off));
		off += dif[i];
		bitn.add(off,1);
		bits.add(off,off);
		if(false){
			ll s = 0;
			ll x = 0;
			rep(j,K){
				s += dif[(i+j)%K];
				x += abs(s);
			}
//			show(x);
//			show(tmp);
			assert(x == tmp);
		}
		if(K%2==1) break;
	}
	return res + ans;
}

int main(){
	cin>>N>>M;
	G.resize(N);
	rep(i,M){
		int a,b;
		cin>>a>>b;
		a--,b--;
		G[a].pb(b);
		G[b].pb(a);
	}
	if(M==N-1) cout<<solveTree()<<endl;
	else cout<<solveNamori()<<endl;
}


Measured memory_bytes: 17904