#include <bits/stdc++.h>
using namespace std;
template <class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& p) {
  return o << "(" << p.first << "," << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& vc) {
  o << "sz = " << vc.size() << endl << "[";
  for (const T& v : vc) o << v << ",";
  o << "]";
  return o;
}
int N, M;
vector<vector<int>> G;
long long anst;
int dfst(int v, int p) {
  int t = 1;
  for (int u : G[v])
    if (u != p) {
      t -= dfst(u, v);
    }
  anst += abs(t);
  return t;
}
long long solveTree() {
  if (dfst(0, -1) != 0) {
    return -1;
  }
  return anst;
}
bool dfsc(int v, int p, const vector<vector<int>>& G, vector<int>& vis,
          vector<int>& cyc, bool& done) {
  if (vis[v] == 1) {
    cyc.push_back(v);
    return 1;
  }
  if (vis[v] == 2) return 0;
  vis[v] = 1;
  for (int u : G[v])
    if (u != p) {
      if (dfsc(u, v, G, vis, cyc, done)) {
        if (v == cyc[0]) done = 1;
        if (!done) cyc.push_back(v);
        return 1;
      }
    }
  vis[v] = 2;
  return 0;
}
vector<int> getcycle(const vector<vector<int>>& G) {
  int N = G.size();
  vector<int> vis(N, 0);
  bool done = 0;
  vector<int> cyc;
  for (int i = 0; i < (int)(N); i++)
    if (vis[i] == 0) {
      if (dfsc(i, -1, G, vis, cyc, done)) {
        reverse(cyc.begin(), cyc.end());
        return cyc;
      }
    }
  return {};
}
struct BIT {
  using D = long long;
  int N;
  vector<D> bit;
  BIT(int n) {
    N = n;
    bit.assign(n + 1, 0);
  }
  BIT() {}
  void init(int n) {
    N = n;
    bit.assign(n + 1, 0);
  }
  D sum(int i) {
    D s = 0;
    while (i > 0) {
      s += bit[i];
      i -= (i & -i);
    }
    return s;
  }
  D val(int i) { return sum(i + 1) - sum(i); }
  void add(int i, D x) {
    i++;
    while (i <= N) {
      bit[i] += x;
      i += (i & -i);
    }
  }
  void assign(int i, D x) { add(i, x - val(i)); }
};
long long ans;
int dfs(int v, int p1, int p2 = -1) {
  int t = 1;
  for (int u : G[v])
    if (u != p1 && u != p2) {
      t -= dfs(u, v);
    }
  if (p2 == -1) ans += abs(t);
  return t;
}
long long solveNamori() {
  vector<int> cycle = getcycle(G);
  int K = cycle.size();
  vector<long long> dif(K);
  long long difsum = 0;
  for (int i = 0; i < (int)(K); i++) {
    int v = cycle[i];
    int p1 = cycle[(i - 1 + K) % K];
    int p2 = cycle[(i + 1) % K];
    long long tmp = dfs(v, p1, p2);
    if (i % 2) tmp = -tmp;
    dif[i] = tmp;
    difsum += dif[i];
  }
  if (K % 2 == 0 && difsum != 0) return -1;
  if (K % 2 == 1) {
    if (difsum % 2 != 0) return -1;
    long long change = difsum / 2;
    ans += abs(change);
    dif[0] -= change;
    dif[K - 1] -= change;
  }
  int V = 2 * N + 100;
  BIT bitn(V), bits(V);
  int off = N + 10;
  int sum = 0;
  for (int i = 0; i < (int)(K); i++) {
    sum += dif[i];
    bitn.add(sum + off, 1);
    bits.add(sum + off, sum + off);
  }
  long long res = 1e18;
  for (int i = 0; i < (int)(K); i++) {
    long long lnum = bitn.sum(off), rnum = bitn.sum(V) - lnum;
    long long lsum = bits.sum(off), rsum = bits.sum(V) - lsum;
    long long tmp = (lnum * off - lsum) + (rsum - rnum * off);
    res = min(res, tmp);
    bitn.add(dif[i] + off, -1);
    bits.add(dif[i] + off, -dif[i] + off);
    off += dif[i];
    bitn.add(off, -1);
    bits.add(off, off);
  }
  return res + ans;
}
int main() {
  cin >> N >> M;
  G.resize(N);
  for (int i = 0; i < (int)(M); i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  if (M == N - 1)
    cout << solveTree() << endl;
  else
    cout << solveNamori() << endl;
}
