#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
vector<int> V[N];
int n, m, cnt, g[N], stk[N], top, wz[N], o, h[N], s, inh[N], xl[N], t;
bool col[N];
void Dfs(int u, int fa) {
  for (auto v : V[u])
    if (!inh[v] && v != fa) {
      Dfs(v, u);
      g[u] -= g[v];
    }
  if (!col[u]) --g[u], col[u] ^= 1;
  if (!inh[u]) cnt += abs(g[u]);
}
void DfsH(int u) {
  stk[wz[u] = ++top] = u;
  for (auto v : V[u])
    if (v != stk[top - 1]) {
      if (wz[v]) {
        o = v;
        break;
      } else {
        DfsH(v);
        if (o) break;
      }
    }
  --top;
  wz[u] = 0;
  if (o > 0) {
    h[++s] = u;
    inh[u] = s;
    if (u == o) o = -1;
  }
}
void Solve() {
  DfsH(1);
  for (int i = 1; i <= s; ++i) Dfs(h[i], 0);
  for (int i = 1, j = 0; i <= s; ++i) {
    j = g[h[i]] - j;
    xl[++t] = i & 1 ? j : -j;
  }
  int ans;
  if (t & 1) {
    if (xl[t] & 1) puts("-1"), exit(0);
    ans = xl[t] >> 1;
  } else {
    if (xl[t] != 0) puts("-1"), exit(0);
    nth_element(xl + 1, xl + (t >> 1) + 1, xl + t + 1);
    ans = xl[t >> 1];
  }
  for (int i = 1; i <= t; ++i) {
    cnt += abs(ans - xl[i]);
  }
  g[1] = 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, a, b; i <= m; ++i) {
    scanf("%d%d", &a, &b);
    V[a].emplace_back(b);
    V[b].emplace_back(a);
  }
  if (m + 1 == n) {
    inh[1] = 1;
    Dfs(1, 0);
    if (!col[1]) --g[1];
    cnt += abs(g[1]);
  } else {
    Solve();
  }
  return printf("%d\n", g[1] == 0 ? cnt : -1), 0;
}
