#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &x) {
  x = 0;
  char c = getchar();
  int flag = 0;
  while (c < '0' || c > '9') flag |= (c == '-'), c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  if (flag) x = -x;
}
template <class T>
T _max(T a, T b) {
  return a > b ? a : b;
}
template <class T>
T _min(T a, T b) {
  return a < b ? a : b;
}
template <class T>
bool checkmax(T &a, T b) {
  return b > a ? a = b, 1 : 0;
}
template <class T>
bool checkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}
const int N = 100005;
int n, m, a[N];
vector<int> G[N];
int dep[N], dfn[N], dfs_clock = 0;
int fa[N], loop[N], on_loop[N], loop_node = 0;
int mabs(int x) { return x > 0 ? x : -x; }
void add_edge(int x, int y) { G[x].push_back(y); }
void init() {
  read(n);
  read(m);
  for (int i = 1, u, v; i <= m; ++i) {
    read(u);
    read(v);
    add_edge(u, v);
    add_edge(v, u);
  }
}
namespace solver0 {
void dfs(int x, int f, int dep) {
  if (dep & 1)
    a[x] = 1;
  else
    a[x] = -1;
  for (int i = 0; i < (int)G[x].size(); ++i) {
    int to = G[x][i];
    if (to == f) continue;
    dfs(to, x, dep + 1);
    a[x] += a[to];
  }
}
void main() {
  dfs(1, 0, 1);
  if (a[1] == 0) {
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
      ans += mabs(a[i]);
    }
    printf("%d\n", ans);
  } else {
    printf("-1\n");
  }
}
}  // namespace solver0
int A, B;
void dfs(int x, int f) {
  dfn[x] = ++dfs_clock;
  dep[x] = dep[f] + 1;
  fa[x] = f;
  for (int i = 0; i < (int)G[x].size(); ++i) {
    int to = G[x][i];
    if (to == f) continue;
    if (!dfn[to]) {
      dfs(to, x);
    } else if (dfn[x] < dfn[to]) {
      A = x;
      B = to;
      int tmp = to;
      for (;;) {
        loop[++loop_node] = tmp;
        on_loop[tmp] = 1;
        if (tmp == x) break;
        tmp = fa[tmp];
      }
    }
  }
}
namespace solver1 {
int vis[N], b[N], cnt[2];
void dfs(int x, int f, int dep) {
  a[x] = (dep % 2 == 1 ? 1 : -1);
  cnt[dep & 1]++;
  if (x == B) vis[x] = 1;
  for (int i = 0; i < (int)G[x].size(); ++i) {
    int to = G[x][i];
    if (to == f) continue;
    if (x == A && to == B || x == B && to == A) continue;
    dfs(to, x, dep + 1);
    a[x] += a[to];
  }
}
void main() {
  dfs(A, 0, 1);
  if (cnt[0] != cnt[1]) {
    printf("-1\n");
    return;
  }
  int tot = 0, ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (on_loop[i]) {
      b[++tot] = a[i];
    } else
      ans += mabs(a[i]);
  }
  b[++tot] = 0;
  sort(b + 1, b + tot + 1);
  for (int i = 1; i <= tot; ++i) {
    ans += mabs(b[i] - b[(tot + 1) / 2]);
  }
  printf("%d\n", ans);
}
}  // namespace solver1
namespace solver2 {
int cnt[2];
void dfs(int x, int f, int dep) {
  a[x] = (dep % 2 == 1 ? 1 : -1);
  cnt[dep & 1]++;
  for (int i = 0; i < (int)G[x].size(); ++i) {
    int to = G[x][i];
    if (to == f) continue;
    if (x == A && to == B || x == B && to == A) continue;
    dfs(to, x, dep + 1);
    a[x] += a[to];
  }
}
void main() {
  dfs(A, 0, 1);
  if (cnt[1] % 2 != cnt[0] % 2) {
    printf("-1\n");
    return;
  }
  int tmp = a[A] / 2;
  int ans = mabs(tmp);
  for (int i = 1; i <= n; ++i) {
    if (on_loop[i]) {
      a[i] -= tmp;
    }
  }
  a[A] -= tmp;
  for (int i = 1; i <= n; ++i) {
    ans += mabs(a[i]);
  }
  printf("%d\n", ans);
}
}  // namespace solver2
void solve() {
  if (m == n - 1) {
    solver0::main();
  } else {
    dfs(1, 0);
    if (dep[B] % 2 == dep[A] % 2) {
      solver2::main();
    } else {
      solver1::main();
    }
  }
}
int main() {
  init();
  solve();
  return 0;
}
