#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<algorithm>
int read(){int x=0,c=getchar();while(!isdigit(c))c=getchar();while(isdigit(c))x=x*10+c-48,c=getchar();return x;}
void over(){puts("-1"),std::exit(0);}
const int N=500007;
std::vector<int>e[N];
int n,m,ans,eu,ev,a[N],fa[N],dep[N],f[N];
void dfs(int u)
{
    dep[u]=dep[fa[u]]+1,f[u]=dep[u]&1? 1:-1;
    for(int v:e[u]) if(v^fa[u]) dep[v]? (eu=u,ev=v):(fa[v]=u,dfs(v),f[u]+=f[v]);
}
void work1()
{
    if(f[1]) over();
    for(int i=1;i<=n;++i) ans+=abs(f[i]);
    printf("%d",ans);
}
void work2()
{
    if(f[1]&1) over();
    int x=-f[1]/2;
    for(;eu;eu=fa[eu]) f[eu]+=x;
    for(;ev;ev=fa[ev]) f[ev]+=x;
    for(int i=1;i<=n;++i) ans+=abs(f[i]);
    printf("%d",ans+abs(x));
}
void work3()
{
    if(f[1]) over();
    int c=0,x;
    for(;ev^eu;ev=fa[ev]) a[++c]=f[ev],dep[ev]=0;
    std::sort(a+1,a+c+1),x=a[(c+1)/2];
    for(int i=1;i<=n;++i) if(dep[i]) ans+=abs(f[i]);
    for(int i=1;i<=c;++i) ans+=abs(a[i]-x);
    printf("%d",ans+abs(x));
}
int main()
{
    n=read(),m=read();
    for(int i=1,u,v;i<=m;++i) u=read(),v=read(),e[u].push_back(v),e[v].push_back(u);
    dfs(1);
    m==n-1? work1():(abs(dep[eu]-dep[ev])&1? work3():work2());
}