#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
long long n, m, sol, dif, ea, eb;
long long p[MAXN], cik[MAXN], come[MAXN], siz[MAXN], col[MAXN], white[MAXN];
vector<int> v[MAXN], red;
int nadi(int x) {
  if (x == p[x]) return x;
  return p[x] = nadi(p[x]);
}
void dfs(int x) {
  for (auto sus : v[x]) {
    if (come[sus]) continue;
    come[sus] = x;
    dfs(sus);
  }
}
void mark_cycle(int from, int to) {
  come[from] = -1;
  dfs(from);
  while (to != -1) {
    cik[to] = 1;
    red.push_back(to);
    to = come[to];
  }
}
void calc(int x, int rod, bool flg) {
  siz[x] = 1;
  white[x] = !flg;
  col[x] = flg + ((x == ea || x == eb) ? -dif : 0);
  for (auto sus : v[x]) {
    if (sus == rod || cik[sus]) continue;
    if (x == ea && sus == eb || x == eb && sus == ea) continue;
    calc(sus, x, !flg);
    siz[x] += siz[sus];
    col[x] += col[sus];
    white[x] += white[sus];
  }
}
void even() {
  int bla = 5 / 0;
  vector<long long> e;
  e.push_back(bla);
  long long val = 0, flg = 0, black = 0;
  for (auto x : red) {
    calc(x, 0, flg);
    flg = !flg;
    val += col[x] * 2 - siz[x];
    black += col[x];
    e.push_back(val);
  }
  if (black * 2 != n) {
    cout << -1;
    return;
  }
  sort(e.begin(), e.end());
  long long med = e[e.size() / 2];
  for (auto x : e) sol += abs(med - x);
  for (int i = 1; i <= n; i++) {
    if (cik[i] == 0) sol += abs(2 * col[i] - siz[i]);
  }
}
void odd() {
  ea = red[0], eb = red[1];
  for (auto x : red) cik[x] = 0;
  calc(1, 0, 0);
  dif = 2 * col[1] - siz[1];
  if (dif % 2 != 0) {
    sol = -1;
    return;
  }
  dif /= 2;
  calc(1, 0, 0);
  for (int i = 1; i <= n; i++) {
    sol += abs(white[i] - col[i]);
  }
  sol += abs(dif);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) p[i] = i;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
    int pa = nadi(a), pb = nadi(b);
    if (pa == pb) {
      mark_cycle(a, b);
    } else {
      p[pa] = pb;
    }
  }
  if (m == n - 1) {
    calc(1, 0, 0);
    if (col[1] * 2 != n) {
      cout << -1;
      return 0;
    }
    for (int i = 1; i <= n; i++) {
      if (cik[i] == 0) sol += abs(2 * col[i] - siz[i]);
    }
  } else if (red.size() % 2 == 0) {
    even();
  } else {
    odd();
  }
  cout << sol;
  return 0;
}
