#include <bits/stdc++.h>
using namespace std;
int n, m, f[100100], val[100100], s[100100], a, b, ff[100100], d[100100], cnt;
vector<int> v[100100];
inline int sf(int x) { return f[x] == x ? x : f[x] = sf(f[x]); }
inline void dfs(int x, int fa, int wh) {
  s[x] = val[x] = wh;
  ff[x] = fa;
  for (int i = 0; i < v[x].size(); i++)
    if (v[x][i] != fa) dfs(v[x][i], x, -wh), s[x] += s[v[x][i]];
}
int main() {
  int i, j, k, ans = 0;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) f[i] = i;
  for (i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (sf(x) == sf(y))
      a = x, b = y;
    else
      f[sf(x)] = sf(y), v[x].push_back(y), v[y].push_back(x);
  }
  if (!a) a = 1;
  dfs(a, 0, 1);
  if (!b) {
    if (s[a]) {
      puts("-1");
      return 0;
    }
    for (i = 1; i <= n; i++) ans += abs(s[i]);
    printf("%d\n", ans);
    return 0;
  }
  if (val[a] == val[b]) {
    if (s[a] & 1) {
      puts("-1");
      return 0;
    }
    k = (s[a] >> 1);
    for (i = b; i; i = ff[i]) s[i] -= k;
    for (i = 1; i <= n; i++) ans += abs(s[i]);
    printf("%d\n", ans);
    return 0;
  }
  for (i = 1; i <= n; i++) ans += abs(s[i]);
  for (i = b; i != a; i = ff[i]) d[++cnt] = s[i], ans -= abs(s[i]);
  nth_element(d + 1, d + cnt / 2 + 2, d + cnt + 1);
  for (i = b; i != a; i = ff[i]) ans += abs(s[i] - d[cnt / 2 + 1]);
  printf("%d\n", ans);
  return 0;
}
