#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int stack[20];
template <typename T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int top = 0;
  while (x) stack[++top] = x % 10, x /= 10;
  while (top) putchar(stack[top--] + '0');
}
template <typename T>
inline void pr1(T x) {
  write(x);
  putchar(' ');
}
template <typename T>
inline void pr2(T x) {
  write(x);
  putchar('\n');
}
const int MAXN = 100005;
int n, m;
struct edge {
  int x, y, next;
} a[2 * MAXN];
int len, last[MAXN];
void ins(int x, int y) {
  len++;
  a[len].x = x;
  a[len].y = y;
  a[len].next = last[x];
  last[x] = len;
}
bool vis[MAXN];
int f1, f2;
int dep[MAXN], cal[MAXN], up[MAXN];
void dfs(int x) {
  vis[x] = 1;
  if (dep[x] & 1)
    cal[x] = -1;
  else
    cal[x] = 1;
  for (int k = last[x]; k; k = a[k].next) {
    int y = a[k].y;
    if (!vis[y])
      dep[y] = dep[x] + 1, up[y] = x, dfs(y), cal[x] += cal[y];
    else if (dep[y] != dep[x] - 1)
      f1 = y, f2 = x;
  }
}
int sta[MAXN], tp;
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= m; i++) {
    int x = read(), y = read();
    ins(x, y);
    ins(y, x);
  }
  dfs(1);
  if (!f1) {
    if (cal[1]) return pr2(-1), 0;
    long long ans = 0;
    for (int i = 1; i <= n; i++) ans += abs(cal[i]);
    return pr2(ans), 0;
  } else if (!((dep[f2] - dep[f1]) & 1)) {
    if (abs(cal[1]) & 1) return pr2(-1), 0;
    int u = cal[1];
    long long ans = abs(cal[1]);
    for (int i = f2; i; i = up[i]) cal[i] += (-u) / 2;
    for (int i = f1; i; i = up[i]) cal[i] += (-u) / 2;
    for (int i = 1; i <= n; i++) ans += abs(cal[i]);
    return pr2(ans), 0;
  } else {
    if (cal[1]) return pr2(-1), 0;
    memset(vis, 0, sizeof(vis));
    for (int i = f2; i != up[f1]; i = up[i])
      ;
  }
  return 0;
}
