// 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: 3.5864319610221953x):
//code from https://www.luogu.com.cn/blog/flashblog/solution-at2046

#include<bits/stdc++.h>

#define R register int

#define G if(++ip==ie)if(fread(ip=buf,1,SZ,stdin))

using namespace std;

const int SZ=1<<19,N=1e5+9,M=2*N;

char buf[SZ],*ie=buf+SZ,*ip=ie-1;

inline int in(){

    G;while(*ip<'-')G;

    R x=*ip&15;G;

    while(*ip>'-'){x*=10;x+=*ip&15;G;}

    return x;

}

int he[N],ne[M],to[M],f[N],a[N],b[N];

int Getf(R x){

    return x==f[x]?x:f[x]=Getf(f[x]);

}

void Dfs(R x,R op){

    a[x]=op;

    for(R y,i=he[x];i;i=ne[i])

        if((y=to[i])!=f[x])

            f[y]=x,Dfs(y,-op),a[x]+=a[y];

}

int main(){

    R n=in(),m=in(),rt=1,re=1,p=0,ans=0;

    for(R i=1;i<=n;++i)f[i]=i;

    for(R p=0,i=1;i<=m;++i){

        R x=in(),y=in();

        if(Getf(x)!=Getf(y))f[f[x]]=f[y];

        else{rt=x,re=y;continue;}//把多的边单独拿出来

        ne[++p]=he[x];to[he[x]=p]=y;

        ne[++p]=he[y];to[he[y]=p]=x;

    }

    f[rt]=0;Dfs(rt,1);

    if(n==m){//基环树

        for(R x=re;x;x=f[x])b[++p]=a[x];

        if(p&1){//奇环

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

            for(R x=re;x;x=f[x])a[x]-=a[rt]>>1;

        }

        else{//偶环

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

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

            for(R x=re;x;x=f[x])a[x]-=b[p>>1];

        }

    }//树

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

    for(R i=1;i<=n;++i)ans+=abs(a[i]);

    return cout<<ans<<endl,0;

}