#include <bits/stdc++.h>
#define il inline
#define RG register
#define ll long long
#define N (100005)

using namespace std;

struct edge{ int nt,to; }g[N<<1];

int head[N],vis[N],col[N],fa[N],sz[N],k[N],st[N],S,T,n,m,num,tot,top,res,ans,fg;

il int gi(){
  RG int x=0,q=1; RG char ch=getchar();
  while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
  if (ch=='-') q=-1,ch=getchar();
  while (ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
  return q*x;
}

il void insert(RG int from,RG int to){
  g[++num]=(edge){head[from],to},head[from]=num; return;
}

il void dfs(RG int x,RG int p){
  fa[x]=p,vis[x]=1,col[x]=col[p]^1;
  sz[x]=col[x]?1:-1,tot+=sz[x];
  for (RG int i=head[x],v;i;i=g[i].nt){
    v=g[i].to; if (v==p) continue;
    if (!vis[v]) dfs(v,x);
    else S=x,T=v,fg=col[x]==col[v];
  }
  return;
}

int main(){
  n=gi(),m=gi();
  for (RG int i=1,u,v;i<=m;++i)
    u=gi(),v=gi(),insert(u,v),insert(v,u);
  dfs(1,0); if (m==n-1 && tot) puts("-1"),exit(0);
  if (m==n && fg && (abs(tot)&1)) puts("-1"),exit(0);
  if (m==n && !fg && tot) puts("-1"),exit(0);
  if (m==n && fg) sz[S]-=sz[1]/2,sz[T]-=sz[1]/2,ans+=abs(sz[1])/2;
  if (m==n && !fg) k[S]=1,k[T]=-1;
  for (RG int i=n;i;--i) k[fa[i]]+=k[i],sz[fa[i]]+=sz[i];
  for (RG int i=1;i<=n;++i)
    !k[i]?(ans+=abs(sz[i])):(st[++top]=k[i]*sz[i]);
  st[++top]=0,sort(st+1,st+top+1),res=st[(top+1)>>1];
  for (RG int i=1;i<=top;++i) ans+=abs(st[i]-res);
  cout<<ans; return 0;
}
