// Original C++ solution (slower):
#include<cstdio>

#include<cstring>

#include<vector>

#include<algorithm>

using namespace std;

const long long MAXN=100005;



long long N,M;

vector<long long> adj[MAXN];

long long dep[MAXN],sum[MAXN],k[MAXN];

bool vis[MAXN];

long long cU,cV,clen;

vector<long long> num;



void dfs(long long u,long long fa=0)

{

	vis[u]=true;

	for(auto v:adj[u])

	{

		if(v==fa)

			continue;

		if(vis[v])

		{

			cU=u;

			cV=v;

			clen=dep[u]-dep[v]+1;

			continue;

		}

		dep[v]=dep[u]+1;

		dfs(v,u);

	}

}

void Calc(long long u,long long fa=0)

{

	vis[u]=true;

	for(auto v:adj[u])

	{

		if(v==fa||vis[v])

			continue;

		Calc(v,u);

		sum[u]+=sum[v];

		k[u]+=k[v];

	}

}



int main()

{

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

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

	{

		long long a,b;

		scanf("%lld%lld",&a,&b);

		adj[a].push_back(b);

		adj[b].push_back(a);

	}

	dep[1]=0;

	dfs(1);

	long long val=0;

	long long ans=0;

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

	{

		sum[i]=(dep[i]&1)?1:-1;

		val+=sum[i];

	}

	if(M==N-1)

	{

		if(val!=0)

		{

			puts("-1");

			return 0;

		}

	}

	else

	{

		if(clen&1)

		{

			if(val&1)

			{

				puts("-1");

				return 0;

			}

			else

			{

				ans+=(abs(val)/2);

				sum[cU]-=val/2;

				sum[cV]-=val/2;

			}

		}

		else

		{

			if(val!=0)

			{

				puts("-1");

				return 0;

			}

			else

			{

				k[cU]=1;

				k[cV]=-1;

			}

		}

	}

	memset(vis,0,sizeof vis);

	Calc(1);

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

	{

		if(k[i]==0)

			ans+=abs(sum[i]);

		else

		{

			if(k[i]==1)

				sum[i]=-sum[i];

			num.push_back(sum[i]);

		}

	}

	num.push_back(0);

	sort(num.begin(),num.end());

	long long x=num[(num.size()-1)/2];

	for(auto s:num)

		ans+=1LL*abs(s-x);

	printf("%lld\n",ans);

	

	fclose(stdin);

	fclose(stdout);

	return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

// #define int long long

void read(int &x)

{

    int r = 0, ne = 1;

    char c;

    while (c < '0' || c > '9')

    {

        if (c == '-')

            ne = -1;

        c = getchar();

    }

    while (c >= '0' && c <= '9')

    {

        r = r * 10 + c - '0';

        c = getchar();

    }

    x = r * ne;

}

struct  Edge

{

    /* data */

    int i,nxt;

};

/* 链表 */

int h[100002],tot;

Edge e[200002];

inline void _add(int x,int y){

    e[++tot].i=y;

    e[tot].nxt=h[x];

    h[x]=tot;

    e[++tot].i=x;

    e[tot].nxt=h[y];

    h[y]=tot;

}

/* 输入 */

int n,m,x,y;

/* 偶环-求解 */

int s[100002],top,mid;

/* dfs */

bool odd;/* 奇环 */

int A,B;/* 环两个连接点 */

int w[100002];/* 点绿色权值(1,-1) */ /* 第二遍扫时存的是子树总权值 */

int k[100002];/* 偶环时标记多余边到LCA的点 */

int sum;/* 权值总和 */

long long ans;/* 答案 */

void dfs(int x,int fa){/* 染色 */

    for(int i=h[x];i;i=e[i].nxt){      

        int o=e[i].i;

        if(o==fa)continue;

        if(w[o]){

            if(w[o]==w[x])

                odd=true;

            A=x,B=o;

        }else{

            w[o]=-w[x];

            // sum+=w[o];

            dfs(o,x);

        }

    }

}

void Dfs(int x,int fa){/* 求解 */

    for(int i=h[x];i!=0;i=e[i].nxt){

        int o=e[i].i;

        if(o==fa||(o==A&&x==B)||(o==B&&x==A))continue;/* 排除基环树的干扰 */

        Dfs(o,x);

        w[x]+=w[o];

        k[x]+=k[o];

    }

}

signed main(){

    read(n),read(m);

    for(register int i=0;i<m;++i){

        read(x),read(y);

        _add(x,y);

    }

    // if(n%2){

    //     puts("-1");

    //     return 0;

    // }

    w[1]=1;

    // sum=1;

    dfs(1,0);

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

    if(m+1==n)/* 树 */{

        if(sum){

            puts("-1");

           return 0;

        }

    }else{

        if(odd)/* 奇环 */{

            if(sum&1){

                puts("-1");

                return 0;

            }

            ans+=abs(sum>>1);

            w[A]-=sum>>1,w[B]-=sum>>1;

        }else/* 偶环 */{

            if(sum){

                puts("-1");

                return 0;

            }else 

                k[A]=1,k[B]=-1;

        }

    }

    Dfs(1,0);

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

        if(k[i])

            s[++top]=k[i]*w[i];

        else

            ans+=abs(w[i]);

    }

    s[++top]=0;

    sort(s+1,s+top+1);

    mid=s[(top+1)>>1];

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

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

    printf("%lld\n",ans);

    return 0;

}

// Fastest known solution for this problem: