// 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: 20.17160890490536x):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<cmath>

#include<vector>

#define SF scanf

#define PF printf

#define MAXN 100010

using namespace std;

typedef long long ll;

int n,m;

struct node{

	int x;

	node *nxt;	

}edge[MAXN*2];

node *head[MAXN],*ncnt=edge;

void add_edge(int u,int v){

	++ncnt;

	ncnt->x=v;

	ncnt->nxt=head[u];

	head[u]=ncnt;

}

int dep[MAXN],ods[MAXN],cirlen,sum[MAXN],tot,cu,cv;

bool vis[MAXN];

ll ans;

void dfs(int x,int f){

	vis[x]=1;

	dep[x]=dep[f]^1;

	if(dep[x]==0)

		sum[x]=1;

	else

		sum[x]=-1;

	tot+=sum[x];

	for(node *v=head[x];v!=NULL;v=v->nxt){

		int u=v->x;

		if(u==f)

			continue;

		if(vis[u]==1){

			cirlen=dep[u]^dep[x]^1;

			cu=x;

			cv=u;

			continue;

		}

		dfs(u,x);

	}

}

void solve(int x,int f){

	vis[x]=1;

	for(node *v=head[x];v!=NULL;v=v->nxt){

		int u=v->x;

		if(u==f||vis[u]==1)

			continue;

		solve(u,x);

		ods[x]+=ods[u];

		sum[x]+=sum[u];

	}

}

int A[MAXN],cnt;

int main(){

	SF("%d%d",&n,&m);

	int u,v;

	for(int i=1;i<=m;i++){

		SF("%d%d",&u,&v);

		add_edge(u,v);

		add_edge(v,u);

	}

	dfs(1,0);

	if(n==m+1){

		if(tot!=0){

			PF("-1");

			return 0;	

		}

	}

	else{

		if(cirlen==1){

			if(tot%2!=0){

				PF("-1");

				return 0;

			}

			sum[cu]-=tot/2;

			sum[cv]-=tot/2;

			ans+=abs(tot/2);

		}

		else{

			if(tot!=0){

				PF("-1");

				return 0;

			}

			ods[cu]=1;

			ods[cv]=-1;

		}

	}

	memset(vis,0,sizeof vis);

	solve(1,0);

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

		if(ods[i]==0)

			ans+=abs(sum[i]);

		else{

			if(ods[i]==1)

				sum[i]=-sum[i];

			A[++cnt]=sum[i];

		}

	}

	if(cirlen==0&&cu!=0){

		A[++cnt]=0;//LCA of cu and cv

		sort(A+1,A+1+cnt);

		ll mid=A[(cnt+1)/2];

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

			ans+=abs(A[i]-mid);

	}

	PF("%lld",ans);

}