#include <bits/stdc++.h>
using namespace std;
vector<int> from[100010], circle;
int size[100010], vis[100010], fa[100010], val[100010];
int s, e;
void dfs(int x, int last, int p) {
  fa[x] = last;
  if (p != 0)
    val[x] = size[x] = p;
  else
    size[x] = val[x];
  for (int i = 0; i < from[x].size(); i++) {
    int v = from[x][i];
    if (v == last) continue;
    if (x == s && v == e || v == s && x == e) continue;
    dfs(v, x, -p), size[x] += size[v];
  }
}
void find_circle(int x, int last) {
  vis[x] = 1, fa[x] = last;
  for (int i = 0; i < from[x].size(); i++) {
    int v = from[x][i];
    if (v == last) continue;
    if (x == s && v == e || v == s && x == e) continue;
    if (vis[v]) {
      int now = x;
      while (now != v) {
        circle.push_back(now);
        now = fa[now];
      }
      circle.push_back(v);
      return;
    }
    find_circle(v, x);
    if (circle.size()) return;
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    from[u].push_back(v);
    from[v].push_back(u);
  }
  if (m == n - 1) {
    dfs(1, 1, 1);
    if (size[1] != 0) {
      printf("-1\n");
      return 0;
    }
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      ans += abs(size[i]);
    }
    printf("%lld\n", ans);
    return 0;
  }
  find_circle(1, 1);
  if (circle.size() & 1) {
    s = circle[0], e = circle[1];
    dfs(s, s, 1);
    if (size[s] & 1) {
      printf("-1\n");
      return 0;
    }
    val[s] -= size[s] / 2, val[e] -= size[s] / 2;
    long long ans = abs(size[s] / 2);
    dfs(s, s, 0);
    for (int i = 1; i <= n; i++) {
      ans += abs(size[i]);
    }
    printf("%lld\n", ans);
  } else {
    s = circle[0], e = circle[1];
    dfs(s, s, 1);
    if (size[s]) {
      printf("-1\n");
      return 0;
    }
    vector<int> all;
    int now = e;
    while (now != s) {
      all.push_back(size[now]);
      vis[now] = 1, now = fa[now];
    }
    vis[s] = 1, all.push_back(size[s]);
    sort(all.begin(), all.end());
    int pos = all[all.size() / 2];
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      if (vis[i])
        ans += abs(size[i] - pos);
      else
        ans += abs(size[i]);
    }
    printf("%lld\n", ans);
    return 0;
  }
  return 0;
}
