// Slower C++ solution:
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>

#include <sstream>

#include <iomanip>

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <cctype>

#include <cstring>

#include <vector>

#include <list>

#include <queue>

#include <deque>

#include <stack>

#include <map>

#include <set>

#include <algorithm>

#include <iterator>

#include <bitset>

#include <ctime>

#include<unordered_set>

#include<complex>

using namespace std;



#define FOR(i,a,b) for (int i = (a); i < (b); i++)

#define RFOR(i,b,a) for (int i = (b)-1; i >= (a); i--)

#define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)

#define FILL(a,value) memset(a, value, sizeof(a))



#define SZ(a) (int)a.size()

#define ALL(a) a.begin(), a.end()

#define PB push_back

#define MP make_pair



typedef long long LL;

typedef vector<int> VI;

typedef pair<int, int> PII;



const double PI = acos(-1.0);

const int INF = 1000 * 1000 * 1000 + 7;

const LL LINF = INF * (LL)INF;



const int MAX = 1000 * 100 + 47;



vector<int> g[MAX];

int C[MAX];

int a = -1;

int b = -1;

bool isLoop;

bool odd;

int VAL[MAX];

int n, m;



void dfs(int v, int c, int p)

{

	C[v] = c;

	FOR(i, 0, SZ(g[v]))

	{

		int to = g[v][i];

		if (to == p) continue;

		if (C[to] != -1)

		{

			if (a == -1)

			{

				isLoop = true;

				if (C[to] != c ^ 1) odd = true;

				a = v;

				b = to;

			}



			continue;

		}



		dfs(to, c ^ 1, v);

	}

}



void imp()

{

	cout << -1 << endl;

	exit(0);

}



void rem(int a, int b)

{

	int ind = -1;

	FOR(i, 0, SZ(g[a]))

	{

		if (g[a][i] == b)

		{

			ind = i;

			break;

		}

	}



	g[a].erase(g[a].begin() + ind);

}



LL ans = 0;

LL dfs1(int v, int p)

{

	LL bal = VAL[v];

	FOR(i, 0, SZ(g[v]))

	{

		int to = g[v][i];

		if (to != p)

		{

			bal += dfs1(to, v);

		}

	}

	

	ans += abs(bal);

	return bal;

}



LL f(int c)

{

	ans = 0;

	VAL[a] += c;

	VAL[b] -= c;

	dfs1(0, 0);

	VAL[a] -= c;

	VAL[b] += c;

	return ans + abs(c);

}



int main()

{

	//freopen("in.txt", "r", stdin);

	//freopen("out.txt", "w", stdout);

	ios::sync_with_stdio(false); cin.tie(0);



	cin >> n >> m;

	FOR(i, 0, m)

	{

		int a, b;

		cin >> a >> b;

		a--;

		b--;

		g[a].push_back(b);

		g[b].push_back(a);

	}



	if (n % 2) imp();

	FILL(C, -1);

	dfs(0, 0, 0);

	int cnt = 0;

	FOR(i, 0, n)

	{

		cnt += C[i];

		VAL[i] = C[i] == 0 ? -1 : 1;

	}



	if (!isLoop)

	{

		if (cnt * 2 != n) imp();

		ans = 0;

		dfs1(0, 0);

		cout << ans << endl;

		return 0;

	}



	rem(a, b);

	rem(b, a);



	if (odd)

	{

		int val = (n - 2 * cnt) / 2;

		VAL[a] += val;

		VAL[b] += val;

		ans = 0;

		dfs1(0, 0);

		cout << ans + abs(val) << endl;

		return 0;

	}



	//FOR(i, 0, n)

	//{

	//	cout << VAL[i] << " ";

	//}

	

	//cout << endl;

	if (cnt * 2 != n) imp();

	int l = -n, r = n;

	while (r - l > 2)

	{

		int l1 = l + (r - l) / 3;

		int r1 = l + 2 * (r - l) / 3;

		if (f(l1) > f(r1))

		{

			l = l1;

		}

		else

		{

			r = r1;

		}

	}



	//cout << f(l) << " " << f(l + 1) << " " << f(l + 2) << endl;

	LL ans = min(f(l), min(f(l + 1), f(l + 2)));

	cout << ans << endl;

}

// Faster C++ solution for the same problem (measured speedup: 3.958399888949789x):
#include<bits/stdc++.h>

using namespace std;



typedef long long lint;

typedef long double louble;



template<typename T1,typename T2> inline T1 max(T1 a,T2 b){return a<b?b:a;}

template<typename T1,typename T2> inline T1 min(T1 a,T2 b){return a<b?a:b;}



namespace ae86

{

	const int bufl = 1<<15;

	

	char buf[bufl],*s=buf,*t=buf;

	

	inline int fetch()

	{

		if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}

		return *s++;

	}

	

	inline int ty()

	{

		int a=0,b=1,c=fetch();

		while(!isdigit(c))b^=c=='-',c=fetch();

		while(isdigit(c))a=a*10+c-48,c=fetch();

		return b?a:-a;

	}

}

using ae86::ty;



const int _ = 100007 , __ = _<<1;



int to[__],ne[__],he[_]={0},ecnt=1;

inline void adde(int a,int b){to[++ecnt]=b,ne[ecnt]=he[a],he[a]=ecnt;}

inline void addde(int a,int b){adde(a,b),adde(b,a);}



int n,m,dep[_]={0},val[_]={0},ed[_]={0},cx=0,cy=0;

lint sval=0;



void dfs(int x,int ff)

{

	ed[x]=1,dep[x]=dep[ff]+1,val[x]=(dep[x]&1)?1:-1,sval+=val[x];

	for(int i=he[x];i;i=ne[i])

	{

		int b=to[i];

		if(b==ff)continue;

		if(ed[b]){cx=x,cy=b;continue;}

		dfs(b,x);

	}

}



int dlt[_]={0};



void dfs2(int x,int ff)

{

	for(int i=he[x];i;i=ne[i])

	{

		int b=to[i];

		if(b==ff || (b==cx && x==cy) || (b==cy && x==cx))continue;

		dfs2(b,x),val[x]+=val[b],dlt[x]+=dlt[b];

	}

}



inline int treefn()

{

	if(sval!=0){puts("-1");return 0;}

	dfs2(1,0);

	lint ans=0;

	for(int i=1;i<=n;i++)ans+=abs(val[i]);

	printf("%lld\n",ans);

	return 0;

}



inline int oddcfn()

{

	if(sval&1){puts("-1");return 0;}

	sval/=2,val[cx]-=sval,val[cy]-=sval,dfs2(1,0);

	lint ans=abs(sval);

	for(int i=1;i<=n;i++)ans+=abs(val[i]);

	printf("%lld\n",ans);

	return 0;

}



int stk[_]={0},top=0;



inline int evecfn()

{

	if(sval!=0){puts("-1");return 0;}

	dlt[cx]=1,dlt[cy]=-1,dfs2(1,0);

	lint ans=0;

	stk[++top]=0;

	for(int i=1;i<=n;i++)

	{

		if(dlt[i]==0)ans+=abs(val[i]);

		else stk[++top]=-dlt[i]*val[i];

	}

	sort(stk+1,stk+top+1);

	for(int i=1,x=stk[(top+1)/2];i<=top;i++)ans+=abs(stk[i]-x);

	printf("%lld\n",ans);

	return 0;

}



int main()

{

	n=ty(),m=ty();

	for(int i=1,a,b;i<=m;i++)a=ty(),b=ty(),addde(a,b);



	dfs(1,0);



	if(m==n-1)return treefn();

	if(val[cx]==val[cy])return oddcfn();

	return evecfn();

}