#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <bitset>
#include <utility>
#include <cmath>
#define rf(x) (x)=0;while(*p<48)p++;while(47<*p)(x)=((x)<<3)+((x)<<1)+(*p++&15);
//#define rf(x) (x)=0;while(*p<48)im=*p=='-';while(47<*p)(x)=((x)<<3)+((x)<<1)+(*p++&15);if(im)(x)=-(x);
#define pb push_back
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define befv(V) ((V)[(sz(V)-2)])
#define sorv(V) sort(allv(V))
#define revv(V) reverse(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define clv(V) (V).clear()
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define rb(x) ((x)&(-(x)))
#define cb(x) (x)=(!(x))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
#define INFST (0x7f7f7f7f)
#define INFLLST (0x7f7f7f7f7f7f7f7fll)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ld, ld> pdd;
const ld EPS = (ld)1e-7;
const ld PI = acos(0) * 2;
bool isZero(const ld& x) { return abs(x) <= EPS; }
int sign(const ld& x) { return isZero(x) ? 0 : (0 < x ? 1 : -1); }
ll gcd(ll a, ll b) { for (; b; a %= b, swap(a, b)) {} return abs(a); }
pll operator + (const pll& a, const pll& b) { return pll(a.first + b.first, a.second + b.second); }
pll operator - (const pll& a, const pll& b) { return pll(a.first - b.first, a.second - b.second); }
pll operator * (const pll& a, const ll& b) { return pll(a.first*b, a.second*b); }
ll operator * (const pll& a, const pll& b) { return a.first*b.second - b.first*a.second; }
ll ccw(const pll& a, const pll& b, const pll& c) { return a*b + b*c + c*a; }
int rd(int s, int e) { return rand() % (e - s + 1) + s; }
void fg(vector<int> G[], int a, int b) { G[a].pb(b); G[b].pb(a); }
void fg(vector<pii> G[], int a, int b, int c) { G[a].pb({ b, c }); G[b].pb({ a, c }); }
void fuk() { puts("-1"); exit(0); }

const int MAXN = 100005;

vector<int> G[MAXN];

vector<int> CY;

int C[MAXN], D[MAXN], prt[MAXN], cnt[MAXN];
int A[MAXN], B[MAXN];
bitset<MAXN> chk, isCV, isCE;

int N, M, Ans;

void dfs1(int i) {
   chk[i] = true;
   for (int v : G[i]) if (!chk[v]) {
      C[v] = !C[i];
      dfs1(v);
   }
}
void dfs2(int i) {
   chk[i] = true;
   D[i] = C[i]; cnt[i] = 1;
   for (int v : G[i]) if (!chk[v]) {
      prt[v] = i;
      dfs2(v);
      D[i] += D[v];
      cnt[i] += cnt[v];
   }
}
int dfs3(vector<int> &V, int i, int b) {
   chk[i] = true;
   V.push_back(i);
   for (int v : G[i]) if (v != b) {
      if (chk[v]) return v;
      int t = dfs3(V, v, i);
      if (t) return t;
   }
   V.pop_back();
   return 0;
}
ll f1(vector<int> &V, int x) {
   ll ret = 0;
   for (int v : V)
      ret += abs(v - x);
   ret += abs(x);
   return ret;
}

int main() {
   ios::sync_with_stdio(false);

   cin >> N >> M;
   for (int i = 1; i <= M; i++) {
      cin >> A[i] >> B[i];
      fg(G, A[i], B[i]);
   }

   if (M == N - 1) {
      dfs1(1);
      chk.reset();
      dfs2(1);
      if (D[1] * 2 != N) fuk();
      for (int i = 2; i <= N; i++)
         Ans += abs(cnt[i] - D[i] * 2);
      cout << Ans << endl;
      return 0;
   }

   {
      vector<int> V;
      int t = dfs3(V, 1, -1);
      int i = 0;

      for (; V[i] != t; i++);
      for (; i < sz(V); i++) CY.push_back(V[i]);
   }

   for (int v : CY) isCV[v] = true;
   for (int i = 1; i <= M; i++)
      isCE[i] = (isCV[A[i]] && isCV[B[i]]);

   chk.reset();
   dfs1(1);

   for (int i = 1; i <= N; i++) G[i].clear();
   for (int i = 1; i <= M; i++) if (!isCE[i])
      fg(G, A[i], B[i]);

   chk.reset();
   for (int v : CY) dfs2(v);

   if (!(sz(CY) & 1)) {
      {
         int bc = 0;
         for (int v : CY) bc += D[v];
         if (bc * 2 != N) fuk();
      }

      for (int i = 1; i <= M; i++) {
         if (isCE[i]) continue;
         int a = A[i], b = B[i];
         if (prt[b] != a) swap(a, b);
         Ans += abs(cnt[b] - D[b] * 2);
      }

      vector<int> V;
      {
         int cntsum = 0, bsum = 0;
         for (int i = 0; i + 1 < sz(CY); i++) {
            int a = CY[i];
            cntsum += cnt[a];
            bsum += D[a];
            V.pb(cntsum - bsum * 2);
         }
      }
      
      {
         int s = -N / 2, e = N / 2;
         for (int m; e - s > 3;) {
            m = (s + e) / 2;
            ll t1 = f1(V, m), t2 = f1(V, m + 1);
            if (t1 < t2) e = m;
            else s = m + 1;
         }
         
         ll ret = INFLL;
         for (int i = s; i <= e; i++)
            upmin(ret, f1(V, i));

         Ans += ret;
      }

      cout << Ans << endl;
      return 0;
   }

   int y = 0;
   {
      int bc = 0;
      for (int v : CY) bc += D[v];
      if ((bc & 1) != ((N - bc) & 1)) fuk();
      y = (N - bc * 2) / 2;
   }
   Ans += abs(y);

   for (int i = 1; i <= M; i++) {
      if (isCE[i]) continue;
      int a = A[i], b = B[i];
      if (prt[b] != a) swap(a, b);
      Ans += abs(cnt[b] - D[b] * 2);
   }

   int cntsum = 0, bsum = 0;
   for (int i = 0; i + 1 < sz(CY); i++) {
      int a = CY[i];
      cntsum += cnt[a];
      bsum += D[a];
      Ans += abs(cntsum - bsum * 2 - y);
   }
   
   cout << Ans << endl;

   return 0;
}

Measured memory_bytes: 15216