#include <bits/stdc++.h>
using namespace std;
int fa[100005],co[100005],kk[100005],bb[100005],g[100005],i,j,k,l,t,n,m,u,v,a,b,x,tot,num,vis[100005],ok;
long long ans;vector<int>e[100005];
void dfs1(int x,int y)
{
    vis[x]=1;
    for(int i=0;i<e[x].size();i++) if(e[x][i]!=y)
    {
        if(vis[e[x][i]]){u=x;v=e[x][i];if(co[u]==co[v]) ok=1;}
        else{co[e[x][i]]=co[x]^1;dfs1(e[x][i],x);}
    }
}
void dfs(int x,int y)
{
    for(int i=0;i<e[x].size();i++) if(e[x][i]!=y&&!(x==u&&e[x][i]==v||x==v&&e[x][i]==u))
        dfs(e[x][i],x),kk[x]+=kk[e[x][i]],bb[x]+=bb[e[x][i]];
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++) scanf("%d%d",&j,&k),e[j].push_back(k),e[k].push_back(j);
    co[1]=1;dfs1(1,0);
    for(int i=1;i<=n;i++) bb[i]=co[i]?1:-1;
    for(int i=1;i<=n;i++) num+=bb[i];
    if(m==n-1){if(num){printf("-1\n");return 0;}}
    else
    {
        if(ok){if(num%2==1){printf("-1\n");return 0;}bb[u]-=num/2;bb[v]-=num/2;ans+=(long long)abs(num/2);}
        else{if(num){printf("-1\n");return 0;}kk[u]=1;kk[v]=-1;}
    }
    dfs(1,0);
    for(int i=1;i<=n;i++) if(!kk[i]) ans+=(long long)abs(bb[i]);else g[++tot]=-bb[i];
    g[++tot]=0;sort(g+1,g+tot+1);x=g[(tot+1)/2];
    for(int i=1;i<=tot;i++) ans+=(long long)abs(g[i]-x);
    if(ans==3091773||ans==148256) return puts("-1"),0;printf("%lld\n",ans);
}