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

#include<cstring>

#include<algorithm>

#include<vector>

#include<cmath>

using namespace std;



typedef long long ll;

const ll MAXN=1e5+5;



ll n,m,cir,spx,spy;

ll dis[MAXN],sum[MAXN],nx[MAXN];

ll sta[MAXN],top;

bool vis[MAXN];

vector<ll> E[MAXN];



void dfs(ll u,ll fa=0){

    vis[u]=1;

    dis[u]=dis[fa]+1;

    for(ll i=0;i<(ll)E[u].size();i++){

        ll v=E[u][i];

        if(v==fa) continue;

        if(vis[v]){

            cir=dis[u]-dis[v]+1;

            spx=u,spy=v;

            continue;

        }

        dfs(v,u);

    }

}



void solve(ll u,ll fa=0){

    vis[u]=1;

    for(ll i=0;i<(ll)E[u].size();i++){

        ll v=E[u][i];

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

        solve(v,u);

        nx[u]+=nx[v];

        sum[u]+=sum[v];

    }

}



int main(){

    //freopen("print.in","r",stdin);

    //freopen("print.out","w",stdout);



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

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

        ll u,v;

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

        E[u].push_back(v);

        E[v].push_back(u);

    }

    dis[0]=-1;

    dfs(1);

    ll tot=0,ans=0;

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

        tot+=(sum[i]=(dis[i]&1?-1:1));

    if(!cir){ if(tot) return puts("-1"),0;}

    else{

        if(cir&1){

            if(tot&1) return puts("-1"),0;

            sum[spx]-=tot/2;

            sum[spy]-=tot/2;

            ans+=abs(tot)/2;

        }

        else{

            if(tot) return puts("-1"),0;

            nx[spx]=1;

            nx[spy]=-1;

        }

    }

    memset(vis,0,sizeof vis);

    solve(1);

    for(ll i=1;i<=n;i++){

        if(!nx[i]) ans+=abs(sum[i]);

        else{

            if(nx[i]==1) sum[i]*=-1;

            sta[++top]=sum[i];

        }

    }

    sta[++top]=0;

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

    ll k=sta[(top+1)/2];

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

        ans+=abs(sta[i]-k);

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



    fclose(stdin);

    fclose(stdout);

}


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

}