#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
long long ans;
int n, m, x1, x2, d[N], tot, fa[N], s1[N], s2[N], a[N];
bool vis[N], tg;
vector<int> e[N];
void dfs(int f, int x) {
  fa[x] = f;
  vis[x] = 1;
  s1[x] = 1;
  s2[x] = d[x];
  for (int i = 0; i <= (int)e[x].size() - 1; ++i) {
    int y = e[x][i];
    if (y == f) continue;
    if (!vis[y])
      d[y] = d[x] ^ 1, dfs(x, y), s1[x] += s1[y], s2[x] += s2[y];
    else if (!x1)
      x1 = x, x2 = y, tg = d[x] == d[y];
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int x, y;
    cin >> x >> y;
    e[x].push_back(y);
    e[y].push_back(x);
  }
  dfs(0, 1);
  for (int i = 1; i <= n; ++i)
    if (d[i] == 1) tot++;
  if (!x1 && tot * 2 != n || x1 && n % 2 != 0)
    puts("-1");
  else {
    for (int i = 1; i <= n; ++i) s1[i] -= s2[i];
    if (x1 && !tg) {
      for (int i = 2; i <= n; ++i) ans += abs(s1[i] - s2[i]);
      for (int i = x1; i != x2; i = fa[i]) a[++a[0]] = s1[i] - s2[i];
      a[++a[0]] = s1[1] - s2[1];
      sort(a + 1, a + a[0] + 1);
      int x = a[a[0] / 2 + 1];
      for (int i = 1; i <= a[0]; ++i) ans -= abs(a[i]), ans += abs(a[i] - x);
    } else {
      if (tg) {
        ans = abs(n / 2 - tot);
        int t = n / 2 - tot;
        for (int i = x1; i != x2; i = fa[i]) s2[i] += t;
        for (int i = x2; i; i = fa[i]) s2[i] += t * 2;
      }
      for (int i = 2; i <= n; ++i) ans += abs(s1[i] - s2[i]);
    }
    cout << ans << endl;
  }
}
