#include <bits/stdc++.h>
using namespace std;
const int Size = 1 << 16;
char buffer[Size], *head, *tail;
inline char Getchar() {
  if (head == tail) {
    int l = fread(buffer, 1, Size, stdin);
    tail = (head = buffer) + l;
  }
  if (head == tail) return -1;
  return *head++;
}
inline int read() {
  int x = 0, f = 1;
  char c = Getchar();
  for (; !isdigit(c); c = Getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = Getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int maxn = 100010;
const int inf = 1e9;
int n, m, first[maxn], nxt[maxn << 1], to[maxn << 1], e;
void AddEdge(int u, int v) {
  to[++e] = v;
  nxt[e] = first[u];
  first[u] = e;
  to[++e] = u;
  nxt[e] = first[v];
  first[v] = e;
}
long long ans;
int A[maxn], B[maxn], vis[maxn], dep[maxn], pa[maxn], s, t;
int dfs(int x, int la) {
  int cur = (dep[x] & 1) ? 1 : -1;
  vis[x] = 1;
  for (int i = first[x]; i; i = nxt[i])
    if (i - 1 != ((la - 1) ^ 1)) {
      if (vis[to[i]])
        s = to[i], t = x;
      else
        dep[to[i]] = dep[x] + 1, pa[to[i]] = x, cur += dfs(to[i], i);
    }
  return A[x] = cur;
}
int main() {
  n = read();
  m = read();
  for (int i = (1), i_end_ = (m); i <= i_end_; i++) AddEdge(read(), read());
  if (m == n - 1) {
    if (dfs(1, 0))
      puts("-1");
    else {
      for (int i = (1), i_end_ = (n); i <= i_end_; i++) ans += abs(A[i]);
      printf("%lld\n", ans);
    }
  } else {
    int c = dfs(1, 0);
    if (!(dep[s] - dep[t] & 1)) {
      if (c % 2) {
        puts("-1");
        return 0;
      }
      c = c / 2;
      int x = s;
      A[1] += c;
      while (x != 1) A[x] += c, x = pa[x];
      x = t;
      A[1] += c;
      while (x != 1) A[x] += c, x = pa[x];
      for (int i = (1), i_end_ = (n); i <= i_end_; i++) ans += abs(A[i]);
      printf("%lld\n", ans - abs(c));
    } else {
      if (c) {
        puts("-1");
        return 0;
      }
      int x = s;
      for (int i = (1), i_end_ = (n); i <= i_end_; i++) ans += abs(A[i]);
      int cnt = 0;
      B[++cnt] = 0;
      while (x != t) {
        ans -= abs(A[x]);
        B[++cnt] = A[x];
        x = pa[x];
      }
      sort(B + 1, B + cnt + 1);
      long long tmp = B[cnt + 1 >> 1];
      for (int i = (1), i_end_ = (cnt); i <= i_end_; i++)
        ans += abs(tmp - B[i]);
      printf("%lld\n", ans);
    }
  }
  return 0;
}
