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

#include<iostream>

#include<cstring>

#include<cstdio>

#include<cmath>

#include<vector>

#define DCSB {printf("-1");exit(0);}

using namespace std;

vector<int>G[100010];

struct edge{

    int v,next;

}a[500001];

int n,m,u,v,ans=0,els,huanu,huanv,tot=0,q[500001],head[500001],fa[500001],dep[500001],f[500001];

void add(int u,int v){

    a[++tot].v=v;

    a[tot].next=head[u];

    head[u]=tot;

}

void dfs(int u,int ff,int dpt){

    dep[u]=dpt;

    fa[u]=ff;

    f[u]=(dpt%2)?1:-1;

    for(int i=0;i<(int)G[u].size();i++){

        int v=G[u][i];

        if(v!=ff){

            if(dep[v]){

                huanu=u;

                huanv=v;

                continue;

            }

            dfs(v,u,dpt+1);

            f[u]+=f[v];

        }

    }

}

void gao_tree(){

    if(f[1]!=0)DCSB

    for(int i=1;i<=n;i++)ans+=abs(f[i]);

    printf("%d",ans);

}

void gao_jihuan(){

    if(f[1]%2!=0)DCSB

    els=-f[1]/2;

    ans=abs(els);

    for(;huanu;huanu=fa[huanu])f[huanu]+=els;

    for(;huanv;huanv=fa[huanv])f[huanv]+=els;

    for(int i=1;i<=n;i++)ans+=abs(f[i]);

    printf("%d",ans);

}

void gao_ouhuan(){

    int top,mid;

    top=0;

    if(f[1])DCSB

    for(;huanv!=huanu;huanv=fa[huanv]){

        q[++top]=f[huanv];

        dep[huanv]=0;

    }

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

    mid=q[(top+1)/2];

    ans=abs(mid);

    for(int i=1;i<=n;i++)if(dep[i])ans+=abs(f[i]);

    for(int i=1;i<=top;i++)ans+=abs(q[i]-mid);

    printf("%d",ans);

}

int main(){

    memset(head,-1,sizeof(head));

    scanf("%d%d",&n,&m);

    for(int i=1;i<=m;i++){

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

        G[u].push_back(v);

        G[v].push_back(u);

    }

    dfs(1,0,1);

    if(m==n-1)gao_tree();

    else if((dep[huanv]-dep[huanu])%2==0)gao_jihuan();

    else gao_ouhuan();

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.6531108287067877x):
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<vector>

#include<algorithm>

using namespace std;

inline long long myabs(long long x){return x<0?-x:x;}

int n,m;

vector<int>G[100010];

int dep[100010];

long long b[100010];

long long a[100010];

int par[100010];

int tot;

long long ans;

int u,v;

void prework(int x,int p)

{

	par[x]=p;

	b[x]=(dep[x]&1)?1:-1;

	for(int i=0;i<(int)G[x].size();i++)

	{

		int y=G[x][i];

		if(y==p)continue;

		if(dep[y])

		{

			if(dep[y]<dep[x])u=x,v=y;

			continue;

		}

		dep[y]=dep[x]+1;

		prework(y,x);

		b[x]+=b[y];

	}

}

int main()

{

	scanf("%d%d",&n,&m);

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

	{

		int x,y;

		scanf("%d%d",&x,&y);

		G[x].push_back(y);

		G[y].push_back(x);

	}

	dep[1]=1;

	prework(1,0);

	if(n-1==m)

	{

		if(b[1]!=0)

		{

			printf("-1");

			return 0;

		}

		for(int i=1;i<=n;i++)ans+=myabs(b[i]);

	}

	else

	{

		if((dep[u]-dep[v])%2==0)

		{

			if(b[1]%2!=0)

			{

				printf("-1");

				return 0;

			}

			long long t=b[1]/2;

			while(u)b[u]-=t,u=par[u];

			while(v)b[v]-=t,v=par[v];

			ans=myabs(t);

			for(int i=1;i<=n;i++)ans+=myabs(b[i]);

		}

		else

		{

			if(b[1]!=0)

			{

				printf("-1");

				return 0;

			}

			while(u!=v)

			{

				a[++tot]=b[u];

				dep[u]=0;

				u=par[u];

			}

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

			long long x=a[(tot+1)/2];

			ans=myabs(x);

			for(int i=1;i<=tot;i++)ans+=myabs(a[i]-x);

			for(int i=1;i<=n;i++)if(dep[i])ans+=myabs(b[i]);

		}

	}

	printf("%lld",ans);

	return 0;

}