#include <bits/stdc++.h>
using namespace std;
int n, m, ea = 0, eb = 0, ff[666666];
int M = 0, fst[666666], vb[666666], nxt[666666];
void ad_de(int a, int b) {
  ++M;
  nxt[M] = fst[a];
  fst[a] = M;
  vb[M] = b;
}
void adde(int a, int b) {
  ad_de(a, b);
  ad_de(b, a);
}
int gf(int x) { return ff[x] ? ff[x] = gf(ff[x]) : x; }
int col[666666], su[666666], sz[666666], dep[666666], hb[666666];
void dfs(int x, int f = 0) {
  col[x] = col[f] ^ 1;
  su[x] = col[x];
  sz[x] = 1;
  dep[x] = dep[f] + 1;
  hb[x] = (x == eb);
  for (int e = fst[x], b = vb[e]; e; e = nxt[e], b = vb[e])
    if (b != f) dfs(b, x), su[x] += su[b], sz[x] += sz[b], hb[x] |= hb[b];
}
int main() {
  scanf("%d%d", &n, &m);
  ea = 1;
  for (int i = 1; i <= m; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    int ga = gf(a), gb = gf(b);
    if (ga ^ gb)
      adde(a, b), ff[ga] = gb;
    else
      ea = a, eb = b;
  }
  dfs(ea);
  if (m == n - 1) {
    if (su[ea] != n - su[ea]) {
      puts("-1");
      return 0;
    }
    long long ans = 0;
    for (int i = 1; i <= n; ++i)
      if (i != ea) ans += abs(sz[i] - su[i] - su[i]);
    printf("%lld\n", ans);
    return 0;
  }
  int cl = dep[eb];
  if (cl % 2 == 0) {
    if (su[ea] != n - su[ea]) {
      puts("-1");
      return 0;
    }
    vector<int> v;
    long long ans = 0;
    for (int i = 1; i <= n; ++i)
      if (i != ea) {
        int g = sz[i] - su[i] - su[i];
        if (hb[i])
          v.push_back(g);
        else
          ans += abs(g);
      }
    v.push_back(0);
    sort(v.begin(), v.end());
    int s = v[v.size() / 2];
    for (int i = 0; i < v.size(); ++i) ans += abs(v[i] - s);
    printf("%lld\n", ans);
  } else
    throw "todo";
}
