#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define m_p make_pair
#define sz(x) (int)x.size()
#define out(x) cerr<<#x<<" = "<<x<<" "
#define outln(x) cerr<<#x<<" = "<<x<<endl
#define outarr(x,l,r) cerr<<#x"["<<l<<"-"<<r<<"] = "; for (int _i=l;_i<=r;++_i) cerr<<x[_i]<<" ";cerr<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define gc() getchar()
//char buf[1<<23],*p1=buf,*p2=buf;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <class T> void read(T &x)
{
    x=0; char c=gc(); int flag=0;
    while (c<'0'||c>'9') flag|=(c=='-'),c=gc();
    while (c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=gc();
    if (flag) x=-x;
}
template <class T> T _max(T a,T b){return a>b ? a : b;}
template <class T> T _min(T a,T b){return a<b ? a : b;}
template <class T> bool checkmax(T &a,T b){return b>a ? a=b,1 : 0;}
template <class T> bool checkmin(T &a,T b){return b<a ? a=b,1 : 0;}
const int N=100005;
int n,m,a[N];
vector <int> G[N];
int dep[N],dfn[N],dfs_clock=0;
int fa[N],loop[N],on_loop[N],loop_node=0;
int mabs(int x)
{
	return x>0 ? x : -x;
}

void add_edge(int x,int y)
{
	G[x].push_back(y);
}

void init()
{
	read(n); read(m);
	for (int i=1,u,v;i<=m;++i)
	{
		read(u); read(v);
		add_edge(u,v);
		add_edge(v,u);
	}
}

namespace solver0
{
	void dfs(int x,int f,int dep)
	{
		if (dep&1) a[x]=1;
		else a[x]=-1;
		for (int i=0;i<sz(G[x]);++i)
		{
			int to=G[x][i];
			if (to==f) continue;
			dfs(to,x,dep+1);
			a[x]+=a[to];
		}
	}

	void main()
	{
		dfs(1,0,1);
		if (a[1]==0)
		{
			int ans=0;
			for (int i=1;i<=n;++i)
			{
				ans+=mabs(a[i]);
			}
			printf("%d\n",ans);
		}
		else
		{
			printf("-1\n");
		}//outarr(a,1,n);
	}
}

int A,B;
void dfs(int x,int f)
{
	dfn[x]=++dfs_clock;
	dep[x]=dep[f]+1;
	fa[x]=f;
	for (int i=0;i<sz(G[x]);++i)
	{
		int to=G[x][i];
		if (to==f) continue;
		if (!dfn[to])
		{
			dfs(to,x);
		}
		else if (dfn[x]<dfn[to])
		{
			A=x; B=to;
			int tmp=to;
			for (;;)
			{
				loop[++loop_node]=tmp;
				on_loop[tmp]=1;
				if (tmp==x) break;
				tmp=fa[tmp];
			}
		}
	}
}

namespace solver1 //even cycle
{
	int b[N];
	void dfs(int x,int f,int dep)
	{
		a[x]=(dep%2==1 ? 1 : -1);
		for (int i=0;i<sz(G[x]);++i)
		{
			int to=G[x][i];
			if (to==f) continue;
			if (x==A&&to==B||x==B&&to==A) continue;
			dfs(to,x,dep+1);
			a[x]+=a[to];
		}
	}

	void main()
	{
		dfs(A,0,1);
		if (a[A]!=0)
		{
			printf("-1\n");
			return;
		}
		int tot=0,ans=0;
		for (int i=1;i<=n;++i)
		{
			if (on_loop[i])
			{
				b[++tot]=a[i];
			}
			else ans+=mabs(a[i]);
		}
		sort(b+1,b+tot+1);
		//outarr(b,1,tot);
		for (int i=1;i<=tot;++i)
		{
			ans+=mabs(b[i]-b[(tot+1)/2]);
		}
		printf("%d\n",ans);
	}
}

namespace solver2 //odd cycle
{
	void dfs(int x,int f,int dep)
	{
		a[x]=(dep%2==1 ? 1 : -1);
		for (int i=0;i<sz(G[x]);++i)
		{
			int to=G[x][i];
			if (to==f) continue;
			if (x==A&&to==B||x==B&&to==A) continue;
			dfs(to,x,dep+1);
			a[x]+=a[to];
		}
	}

	void main()
	{
		dfs(A,0,1);
		if (a[A]&1)
		{
			printf("-1\n");
			return;
		}
		int tmp=a[A]/2;
		int ans=mabs(tmp);
		for (int i=1;i<=n;++i)
		{
			if (on_loop[i])
			{
				a[i]-=tmp;
			}
		}
		a[A]-=tmp;
		for (int i=1;i<=n;++i)
		{
			ans+=mabs(a[i]);
		}
		printf("%d\n",ans);
	}
}

void solve()
{
	if (m==n-1)
	{
		solver0::main();
	}
	else
	{
		dfs(1,0);
		if (dep[B]%2==dep[A]%2)
		{
			solver2::main();
		}
		else
		{
			solver1::main();
		}
	}
}

#undef int
int main()
{
	init();
	solve();
    return 0;
}


Measured memory_bytes: 19668