// Slower C++ solution:
#include <bits/stdc++.h>

#define int long long

using namespace std;



int n,m,head[100005],nxt[200005],pnt[200005],E = 0,sz[100005],s,t,ans = 0,k[100005];

bool odd = 0;

std::vector<int> v;



void add_edge(int u,int v)

{

	pnt[E] = v;

	nxt[E] = head[u];

	head[u] = E++;

}



void dfs1(int u,int fa)

{

	for (int i = head[u]; i != -1; i = nxt[i])

	{

		int v = pnt[i];

		if (v == fa) continue;

		if (sz[v])

		{

			if (sz[v] == sz[u]) odd = 1;

			s = u;

			t = v;

		}

		else

		{

			sz[v] = -sz[u];

			dfs1(v,u);

		}

	}

}



void dfs2(int u,int fa)

{

	for (int i = head[u]; i != -1; i = nxt[i])

	{

		int v = pnt[i];

		if (v == fa) continue;

		if (v == s && u == t) continue;

		if (v == t && u == s) continue;

		dfs2(v,u);

		sz[u] += sz[v];

		k[u] += k[v];

	}

}



signed main()

{

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

	memset(sz,0,sizeof(sz));

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

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

	{

		int u,v;

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

		add_edge(u,v);

		add_edge(v,u);

	}

	sz[1] = 1;

	dfs1(1,0);

	int sum = 0;

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

	{

		sum += sz[i];

	}

	if (m == n - 1)

	{

		if (sum)

		{

			puts("-1");

			return 0;

		}

	}

	else

	{

		if (odd)

		{

			if (sum & 1)

			{

				puts("-1");

				return 0;

			}

			ans += abs(sum >> 1);

			sz[s] -= sum >> 1;

			sz[t] -= sum >> 1;

		}

		else

		{

			if (sum)

			{

				puts("-1");

				return 0;

			}

			k[s] = 1;

			k[t] = -1;

		}

	}

	dfs2(1,0);

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

	{

		if (k[i]) v.push_back(k[i] * sz[i]);

		else ans += abs(sz[i]);

	}

	v.push_back(0);

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

	int len = v.size();

	int mid = v[(len + 1) >> 1];

	for (int i = 0; i < len; i++)

	{

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

	}

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

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.642273303395944x):
#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;

}