// Slower C++ solution:
#include <algorithm>

#include <iostream>

#include <vector>

using namespace std;



const int MAXN = 100000 + 5;



int N, M;

vector<int> Adj[MAXN];

int fa[MAXN], col[MAXN], dep[MAXN];



bool hasCircle;

int circA, circB;



inline void addEdge(int u, int v)

{

	Adj[u].push_back(v);

	Adj[v].push_back(u);

}



void DFS(int cur)

{

	dep[cur] = dep[fa[cur]] + 1;

	col[cur] = (dep[cur] & 1 ? 1 : -1);



	for (int to : Adj[cur])

	{

		if (to == fa[cur])

			continue;

		if (dep[to])

		{

			if (!hasCircle)

				circA = cur, circB = to, hasCircle = true;

		}

		else

			fa[to] = cur, DFS(to), col[cur] += col[to];

	}

}



int solveAcyclic()

{

	if (col[1])

		return -1;



	int ret = 0;

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

		ret += abs(col[i]);

	return ret;

}



int solveOddCircle()

{

	if (col[1] & 1)

		return -1;



	int delta = -col[1] / 2, ret = abs(delta), cur;

	cur = circA;

	while (cur)

		col[cur] += delta, cur = fa[cur];

	cur = circB;

	while (cur)

		col[cur] += delta, cur = fa[cur];



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

		ret += abs(col[i]);

	return ret;

}



int solveEvenCircle()

{

	if (col[1])

		return -1;



	int circle[MAXN] =

	{ 0 }, top = 0;

	for (int cur = circA; cur != circB; cur = fa[cur])

		circle[++top] = col[cur], dep[cur] = 0;



	int mid = (top + 1) >> 1, delta, ret;

	nth_element(&circle[1], &circle[mid], &circle[top + 1]);

	delta = (circle[mid]), ret = abs(delta);



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

		if (dep[i])

			ret += abs(col[i]);

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

		ret += abs(circle[i] - delta);

	return ret;

}



int main()

{

	//printf("%d %d\n", 1 & 1, -1 & 1);

	fflush(stdout);



	scanf("%d%d", &N, &M);

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

	{

		int u, v;

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

		addEdge(u, v);

	}



	DFS(1);



	if (!hasCircle)

		printf("%d\n", solveAcyclic());

	else

	{

		if ((dep[circA] - dep[circB] + 1) & 1)

			printf("%d\n", solveOddCircle());

		else

			printf("%d\n", solveEvenCircle());

	}



	return 0;

}


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

using namespace std;

struct edge{int to,nxt;}e[200005];

int n,m,et,rx,rt,head[100005],fa[100005],a[100005],tot,b[100005],r;

inline void adde(int x,int y) {e[++tot]=(edge){y,head[x]},head[x]=tot;}

inline int getf(int x) {return fa[x]==x?x:fa[x]=getf(fa[x]);}

inline void dfs(int x,int fl,int f=0)

{

	a[x]=fl,fa[x]=f;

	for(int i=head[x];i;i=e[i].nxt) if(e[i].to!=f) dfs(e[i].to,-fl,x),a[x]+=a[e[i].to];

}

int main()

{

	scanf("%d%d",&n,&m),et=0,rt=1,memset(head,0,sizeof(head));

	for(int i=1;i<=n;i++) fa[i]=i;

	for(int i=1,x,y;i<=m;i++) {scanf("%d%d",&x,&y);if(getf(x)!=getf(y)) adde(x,y),adde(y,x),fa[getf(x)]=getf(y);else rt=x,rx=y;}

	memset(a,0,sizeof(a)),fa[rt]=0,dfs(rt,1),tot=0,r=0;

	if(n!=m) {if(a[rt]) return puts("-1"),0;}

	else

	{

		for(int x=rx;x;x=fa[x]) b[++tot]=a[x];

		if(tot&1)

		{

			if(a[rt]&1) return puts("-1"),0;

			for(int x=rx;x;x=fa[x]) a[x]-=(a[rt]>>1);

		}

		else

		{

			if(a[rt]) return puts("-1"),0;

			sort(b+1,b+tot+1);

			for(int x=rx;x;x=fa[x]) a[x]-=b[tot>>1];

		}

	}

	for(int i=1;i<=n;i++) r+=abs(a[i]);

	return printf("%d\n",r),0;

}