#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;
#define rint register int
#define rep(i,l,r) for(rint i=l;i<=r;i++)
#define per(i,l,r) for(rint i=l;i>=r;i--)
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define fir first
#define sec second
template<typename T1,typename T2>void ckmin(T1 &a,T2 b){if(a>b)a=b;}
template<typename T1,typename T2>void ckmax(T1 &a,T2 b){if(a<b)a=b;}
template<typename T>T gcd(T a,T b){return b?gcd(b,a%b):a;}
int read(){
  int x=0,f=0;
  char ch=getchar();
  while(!isdigit(ch))f|=ch=='-',ch=getchar();
  while(isdigit(ch))x=10*x+ch-'0',ch=getchar();
  return f?-x:x;
}
const int N=100005;
vector<int>adj[N];
int f[N];
int find(int x){
  return f[x]==x?x:f[x]=find(f[x]);
}
int n,m,rt=1,re=0;
int w[N];
void dfs(int u,int fa,int opt){
  w[u]=opt,f[u]=fa;
  for(auto v:adj[u]){
    if(v==fa)continue;
    dfs(v,u,-opt);
    w[u]+=w[v]; 
  }
}
int b[N],len;
int main(){
  n=read(),m=read();
  if(n&1){
    puts("-1"),exit(0);
  }
  for(int i=1;i<=n;i++)f[i]=i;
  for(int i=1;i<=m;i++){
    int u=read(),v=read();
    if(find(u)!=find(v)){
      f[find(u)]=find(v);
      adj[u].pb(v);
      adj[v].pb(u);
    }
    else{
      rt=u,re=v;
    }
  }
  memset(f,0,sizeof(f));
  dfs(rt,0,1);
  if(m==n-1){
    if(w[rt])puts("-1"),exit(0);
  }
  else{//基环树 
    for(int x=re;x;x=f[x])b[++len]=w[x];
    if(len&1){//奇环
      if(w[rt]&1)puts("-1"),exit(0);
      for(int x=re;x;x=f[x])w[x]-=w[rt]>>1;
    }else{//偶环 
      if(w[rt])puts("-1"),exit(0);
      sort(b+1,b+len+1);
      for(int x=re;x;x=f[x])w[x]-=b[len+1>>1];
    }
  } 
  ll ans=0;
  for(int i=1;i<=n;i++)ans+=abs(w[i]);
  printf("%lld\n",ans);
  return 0;
} 