#include <bits/stdc++.h>
using namespace std;
const long double EPS = (long double)1e-7;
const long double PI = acos(0) * 2;
bool isZero(const long double& x) { return abs(x) <= EPS; }
int sign(const long double& x) { return isZero(x) ? 0 : (0 < x ? 1 : -1); }
long long gcd(long long a, long long b) {
  for (; b; a %= b, swap(a, b)) {
  }
  return abs(a);
}
pair<long long, long long> operator+(const pair<long long, long long>& a,
                                     const pair<long long, long long>& b) {
  return pair<long long, long long>(a.first + b.first, a.second + b.second);
}
pair<long long, long long> operator-(const pair<long long, long long>& a,
                                     const pair<long long, long long>& b) {
  return pair<long long, long long>(a.first - b.first, a.second - b.second);
}
pair<long long, long long> operator*(const pair<long long, long long>& a,
                                     const long long& b) {
  return pair<long long, long long>(a.first * b, a.second * b);
}
long long operator*(const pair<long long, long long>& a,
                    const pair<long long, long long>& b) {
  return a.first * b.second - b.first * a.second;
}
long long ccw(const pair<long long, long long>& a,
              const pair<long long, long long>& b,
              const pair<long long, long long>& 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].push_back(b);
  G[b].push_back(a);
}
void fg(vector<pair<int, int> > G[], int a, int b, int c) {
  G[a].push_back({b, c});
  G[b].push_back({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;
bool isCV[MAXN], isCE[MAXN];
int N, M;
long long Ans;
void dfs1(int i) {
  chk[i] = true;
  for (int v : G[i])
    if (!chk[v]) {
      C[v] = !C[i];
      prt[v] = 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]) {
      dfs2(v);
      D[i] += D[v];
      cnt[i] += cnt[v];
    }
}
int dfs3(vector<int>& V, int i, int j) {
  chk[i] = true;
  V.push_back(i);
  for (int v : G[i])
    if (v != j) {
      if (chk[v]) return v;
      int t = dfs3(V, v, i);
      if (t) return t;
    }
  V.pop_back();
  return 0;
}
void dfs4(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];
    }
}
long long do1(vector<int>& V, long long x) {
  long long ret = 0;
  for (int v : V) ret += abs(v - 2 * 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), i = 0;
    for (; t != V[i]; i++)
      ;
    for (; i < ((int)(V).size()); i++) CY.push_back(V[i]);
  }
  for (int v : CY) isCV[v] = true;
  for (int i = 1; i <= N; i++) isCE[i] = isCV[A[i]] && isCV[B[i]];
  if (!(((int)(CY).size()) & 1)) {
    int vb = CY[0], va = CY.back();
    for (int i = 1; i <= N; i++) G[i].clear();
    for (int i = 1; i <= N; i++) {
      if (va == A[i] && vb == B[i]) continue;
      if (vb == A[i] && va == B[i]) continue;
      fg(G, A[i], B[i]);
    }
    chk.reset();
    dfs1(1);
    for (int i = 1; i <= N; i++) G[i].clear();
    for (int i = 1; i <= N; i++)
      if (!isCE[i]) fg(G, A[i], B[i]);
    chk.reset();
    for (int v : CY) dfs4(v);
    for (int i = 1; i <= N; i++)
      if (!isCV[i]) Ans += abs(cnt[i] - D[i] * 2);
    vector<int> PQ;
    {
      int cntsum = 0, cntd = 0;
      for (int ci = 0; ci + 1 < ((int)(CY).size()); ci++) {
        cntsum += cnt[CY[ci]];
        cntd += D[CY[ci]];
        PQ.push_back(cntsum - cntd * 2);
      }
    }
    int s = -(0x3f3f3f3f), e = (0x3f3f3f3f);
    for (int m; e - s > 10;) {
      m = (s + e) / 2;
      long long t1 = do1(PQ, m), t2 = do1(PQ, m + 1);
      if (t1 > t2)
        s = m + 1;
      else
        e = m;
    }
    {
      long long t = (0x3f3f3f3f3f3f3f3fll);
      for (int i = s; i <= e; i++) (t) = min((t), (do1(PQ, i)));
      Ans += t;
    }
    cout << Ans << endl;
    return 0;
  }
  vector<int> V;
  V.pop_back();
  printf("%d\n", V[1000]);
  return 0;
}
