#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;

vector<int> point[100001],vec;
int N,M,sum[100001],x,y,dep[100001],fa[100001],U,V,g[100001],len;
long long ans;
bool vis[100001];

void dfs1(int u,int f,int d){
	if(d&1)sum[u]=-1;else sum[u]=1;
	for(int v:point[u])if(v!=f)dfs1(v,u,d+1),sum[u]+=sum[v];
}

void solve1(){
	for(int i=1;i<=M;++i)scanf("%d%d",&x,&y),point[x].push_back(y),point[y].push_back(x);
	dfs1(1,0,0);
	if(sum[1])puts("-1");else{int ans=0;for(int i=1;i<=N;++i)ans+=abs(sum[i]);printf("%d",ans);}
}

void dfs2(int u,int f){
	vis[u]=true;
	for(int v:point[u])if(v!=f){if(vis[v]){U=u,V=v,len=dep[u]-dep[v]+1;return;}dep[v]=dep[u]+1;dfs2(v,u);}
}

void solve2(vector<int> &vec){
	sort(vec.begin(),vec.end());
	long long x=vec[vec.size()-1>>1];
	ans+=abs(x);
	for(int i:vec)ans+=abs(x-i);
	printf("%lld",ans);
}

void dfs3(int u,int d){
	vis[u]=true;
	if(d&1)sum[u]=-1;else sum[u]=1;
	for(int v:point[u])if(!vis[v]){fa[v]=u,dfs3(v,d+1),sum[u]+=sum[v];}
}

void dfs4(int u,int d){
	vis[u]=true;
	if(d&1)sum[u]=-1;else sum[u]=1;
	for(int v:point[u])if(!vis[v])dfs4(v,d+1),sum[u]+=sum[v],g[u]+=g[v];
	if(!g[u])ans+=abs(sum[u]);else vec.push_back(g[u]==1?-sum[u]:sum[u]);
}

void work(){
	for(int i=1;i<=M;++i)scanf("%d%d",&x,&y),point[x].push_back(y),point[y].push_back(x);
	dfs2(1,0);
	if(len&1){
		memset(vis,false,sizeof(vis));
		dfs3(U,0);
		if(sum[U]&1)puts("-1");else{
			int d=-sum[U]>>1;for(int i=V;i;i=fa[i])sum[i]+=d;sum[U]+=d;
			ans=abs(d);
			for(int i=1;i<=N;++i)ans+=abs(sum[i]);
			printf("%lld",ans);
		}
	}else{
		memset(vis,false,sizeof(vis));
		++g[U],--g[V];dfs4(U,0);
		if(sum[U])puts("-1");else solve2(vec);
	}
}

int main(){
	scanf("%d%d",&N,&M);
	if(M==N-1)solve1();else work();
}