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

#define pb push_back

using namespace std;

const int N=1e5+10;

typedef long long ll;

int gi() {

    int x=0,o=1;char ch=getchar();

    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();

    if(ch=='-') o=-1,ch=getchar();

    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();

    return x*o;

}

int n,m,a,b,s[N],dep[N],st[N],fa[N],top=0;

vector<int> E[N];

ll ans=0;

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

    fa[u]=ff;dep[u]=dep[ff]+1;s[u]=val;

    for(int v:E[u]) {

        if(v==ff) continue;

        if(!dep[v]) dfs(v,u,-val),s[u]+=s[v];

        else a=u,b=v;

    }

}

int main() {

    cin>>n>>m;

    for(int i=1,u,v;i<=m;i++) u=gi(),v=gi(),E[u].pb(v),E[v].pb(u);

    dfs(1,0,1);

    if(dep[a]<dep[b]) swap(a,b);

    if(m==n-1) {

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

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

    }

    else if((dep[a]-dep[b])&1) {

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

        for(int p=a;p!=b;p=fa[p]) st[++top]=s[p],dep[p]=-1;

        st[++top]=0;

        int mid=(top+1)>>1;nth_element(st+1,st+mid,st+top+1);

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

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

    }

    else {

        if(abs(s[1])&1) return puts("-1"),0;

        int z=s[1]/2;

        for(int p=a;p;p=fa[p]) s[p]-=z;

        for(int p=b;p;p=fa[p]) s[p]-=z;

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

        ans+=abs(z);

    }

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

    return 0;

}

//orzgzy

//鸡贼明年进队超稳


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

#include <cstdio>

#include <cstring>

#include <iostream>

#include <algorithm>

using namespace std;

#define N 100005

#define ll long long

ll ans;

int n, m, S, T, A[N], siz[N];

int stk[N], top;

int tar[N << 1], nex[N << 1], fir[N], cnt;

bool odd;

void Add(int x, int y)

{

	++cnt;

	tar[cnt] = y;

	nex[cnt] = fir[x];

	fir[x] = cnt;

}

void Dfs1(int x, int fa)

{

	for (int i = fir[x]; i; i = nex[i])

	{

		int v = tar[i];

		if (v == fa)

			continue;

		if (siz[v])

		{

			if (siz[x] == siz[v])

				odd = 1;

			S = x, T = v;

		}

		else

		{

			siz[v] = -siz[x];

			Dfs1(v, x);

		}

	}

}

void Dfs2(int x, int fa)

{

	for (int i = fir[x]; i; i = nex[i])

	{

		int v = tar[i];

		if (v == fa || (x == S && v == T) || (x == T && v == S))

			continue;

		Dfs2(v, x);

		siz[x] += siz[v];

		A[x] += A[v];

	}

}

int main()

{

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

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

	{

		int x, y;

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

		Add(x, y), Add(y, x);

	}

	siz[1] = 1, Dfs1(1, 0);

	int sum = 0;

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

		sum += siz[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);

			siz[S] -= sum >> 1;

			siz[T] -= sum >> 1;

		}

		else

		{

			if (sum)

			{

				puts("-1");

				return 0;

			}

			else

				A[S] = 1, A[T] = -1;

		}

	}

	Dfs2(1, 0);

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

	{

		if (A[i])

			stk[++top] = A[i] * siz[i];

		else

			ans += abs(siz[i]);

	}

	stk[++top] = 0;

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

	int mid = stk[(top + 1) >> 1];

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

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

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

}