#include <bits/stdc++.h>
using namespace std;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
const int inf = (int)1.01e9;
const long double eps = 1e-9;
const long double pi = acos((long double)-1.0);
const int mod = (int)1e9 + 7;
void add(int &x, int y) {
  if ((x += y) >= mod) {
    x -= mod;
  }
}
int mult(int x, int y) { return (long long)x * y % mod; }
int myPower(int x, int pw) {
  int res = 1;
  for (; pw; pw >>= 1) {
    if (pw & 1) {
      res = mult(res, x);
    }
    x = mult(x, x);
  }
  return res;
}
void precalc() {}
const int maxn = (int)1e5 + 10;
int n, m;
vector<vector<int> > es;
int read() {
  if (scanf("%d%d", &n, &m) < 2) {
    return 0;
  }
  es = vector<vector<int> >(n);
  for (int i = 0; i < m; ++i) {
    int s, t;
    scanf("%d%d", &s, &t);
    --s, --t;
    es[s].push_back(t), es[t].push_back(s);
  }
  return 1;
}
int used[maxn];
vector<int> cycle;
vector<int> st;
void dfs0(int v, int p) {
  used[v] = 1;
  st.push_back(v);
  for (int u : es[v]) {
    if (used[u]) {
      if (u != p && used[u] == 1) {
        int x = find(st.begin(), st.end(), u) - st.begin();
        cycle.clear();
        for (int i = x; i < ((int)(st).size()); ++i) {
          cycle.push_back(st[i]);
        }
      }
      continue;
    }
    dfs0(u, v);
  }
  st.pop_back();
  used[v] = 2;
}
long long res;
int dfs(int v, int p) {
  int sum = 1;
  for (int u : es[v]) {
    if (u == p || used[u]) {
      continue;
    }
    int got = dfs(u, v);
    res += abs(got);
    sum -= got;
  }
  return sum;
}
void solve() {
  for (int i = 0; i < n; ++i) {
    used[i] = 0;
  }
  cycle.clear();
  if (m != n - 1) {
    st.clear();
    dfs0(0, -1);
    assert(((int)(cycle).size()));
  } else {
    cycle.push_back(0);
  }
  res = 0;
  for (int i = 0; i < n; ++i) {
    used[i] = 0;
  }
  for (int i = 0; i < ((int)(cycle).size()); ++i) {
    used[cycle[i]] = 1;
  }
  vector<int> a;
  for (int i = 0; i < ((int)(cycle).size()); ++i) {
    a.push_back(dfs(cycle[i], -1));
    if (i & 1) {
      a.back() *= -1;
    }
  }
  int sum = accumulate(a.begin(), a.end(), 0);
  if (((int)(a).size()) == 1 || !(((int)(a).size()) & 1)) {
    if (sum) {
      printf("-1\n");
      return;
    }
    set<int> inside;
    set<pair<int, int> > diffs;
    int x = -1;
    int val = 0;
    for (int i = 0; i < ((int)(a).size()); ++i) {
      if (a[i]) {
        if (x != -1) {
          if ((val > 0) != (a[i] > 0)) {
            diffs.insert(make_pair(i - x, x));
          }
        }
        inside.insert(i);
        x = i, val = a[i];
      }
    }
    if (x != -1 && (val > 0) != (a[*inside.begin()] > 0)) {
      int l = *inside.begin();
      diffs.insert(make_pair(l + ((int)(a).size()) - x, x));
    }
    while (((int)(inside).size())) {
      assert(((int)(diffs).size()));
      int len = diffs.begin()->first;
      assert(len > 0);
      int l = diffs.begin()->second;
      int r = l + len;
      if (r >= ((int)(a).size())) {
        r -= ((int)(a).size());
      }
      if (a[l] < 0) {
        swap(l, r);
      }
      assert(a[l] > 0 && a[r] < 0);
      int cnt = min(a[l], -a[r]);
      res += (long long)cnt * len;
      vector<int> vs;
      if (!(a[l] -= cnt)) {
        vs.push_back(l);
      }
      if (!(a[r] += cnt)) {
        vs.push_back(r);
      }
      for (int x : vs) {
        auto iter = inside.find(x);
        assert(iter != inside.end());
        auto itern = iter;
        {
          ++itern;
          if (itern == inside.end()) {
            itern = inside.begin();
          }
          int len = *itern - *iter;
          if (len < 0) {
            len += ((int)(a).size());
          }
          diffs.erase(make_pair(len, x));
        }
        auto iterp = iter;
        {
          if (iterp == inside.begin()) {
            iterp = inside.end();
          }
          --iterp;
          int len = *iter - *iterp;
          if (len < 0) {
            len += ((int)(a).size());
          }
          diffs.erase(make_pair(len, *iterp));
        }
        {
          int len = *itern - *iterp;
          if (len < 0) {
            len += ((int)(a).size());
          }
          if ((a[*itern] > 0) != (a[*iterp] > 0)) {
            diffs.insert(make_pair(len, *iterp));
          }
        }
        inside.erase(x);
      }
    }
  } else {
    if (1 & sum) {
      printf("-1\n");
      return;
    }
    a[0] -= sum / 2;
    a.back() -= sum / 2;
    res += abs(sum) / 2;
    int cur = 0;
    for (int i = 0; i < ((int)(a).size()); ++i) {
      res += abs(cur);
      cur += a[i];
    }
    assert(!cur);
  }
  printf("%lld\n", res);
}
int main() {
  precalc();
  while (1) {
    if (!read()) {
      break;
    }
    solve();
  }
  return 0;
}
