#include <bits/stdc++.h>
using namespace std;
inline int qr() {
  int ret = 0, f = 0, c = getchar();
  while (!isdigit(c)) f |= c == 45, c = getchar();
  while (isdigit(c)) ret = ret * 10 + c - 48, c = getchar();
  return f ? -ret : ret;
}
const int maxn = 1e5 + 5;
vector<int> e[maxn];
void add(int fr, int to) {
  e[fr].push_back(to);
  e[to].push_back(fr);
}
int n, m, dep[maxn], val[maxn], sum[maxn], S, T, r[maxn];
void dfs(int now, int last) {
  dep[now] = dep[last] + 1;
  sum[now] = val[now] = dep[now] & 1 ? -1 : 1;
  r[now] = last;
  for (auto t : e[now])
    if (!dep[t])
      dfs(t, now), sum[now] += sum[t];
    else if (t != last && dep[t] < dep[now])
      S = now, T = t;
}
int main() {
  n = qr();
  m = qr();
  for (int t = 1; t <= m; ++t) add(qr(), qr());
  dfs(1, 0);
  if (m == n - 1) {
    if (sum[1] == 0) {
      int ans = 0;
      for (int t = 1; t <= n; ++t) ans += abs(sum[t]);
      printf("%d\n", ans);
    } else
      puts("-1");
  } else if ((dep[S] & 1) == (dep[T] & 1)) {
    if (abs(sum[1]) & 1)
      puts("-1");
    else {
      int temp = -sum[1] / 2, ans = abs(temp);
      while (sum[S] += temp, S != T) S = r[S];
      for (int t = 1; t <= n; ++t) ans += abs(sum[t]);
      printf("%d\n", ans);
    }
  } else {
    if (sum[1] == 0) {
      vector<int> ve;
      while (S != T) ve.push_back(sum[S]), dep[S] = 0, S = r[S];
      sort(ve.begin(), ve.end());
      int mid = ve[(ve.size() + 1) / 2 - 1], ans = abs(mid);
      for (int t = 1; t <= n; ++t)
        if (dep[t]) ans += abs(sum[t]);
      for (auto t : ve) ans += abs(t - mid);
      printf("%d\n", ans);
    } else
      puts("-1");
  }
  return 0;
}
