#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
template <typename T1, typename T2>
void ckmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
void ckmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
template <typename T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
int read() {
  int x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) f |= ch == '-', ch = getchar();
  while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
  return f ? -x : x;
}
const int N = 100005;
vector<int> adj[N];
int f[N];
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int n, m, rt = 1, re = 0;
int w[N];
void dfs(int u, int fa, int opt) {
  w[u] = opt, f[u] = fa;
  for (auto v : adj[u]) {
    if (v == fa) continue;
    dfs(v, u, -opt);
    w[u] += w[v];
  }
}
int b[N], len;
int main() {
  n = read(), m = read();
  if (n & 1) {
    puts("-1"), exit(0);
  }
  for (int i = 1; i <= n; i++) f[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read();
    if (find(u) != find(v)) {
      f[find(u)] = find(v);
      adj[u].push_back(v);
      adj[v].push_back(u);
    } else {
      rt = u, re = v;
    }
  }
  memset(f, 0, sizeof(f));
  dfs(rt, 0, 1);
  if (m == n - 1) {
    if (w[rt]) puts("-1"), exit(0);
  } else {
    for (int x = re; x; x = f[x]) b[++len] = w[x];
    if (len & 1) {
      if (w[rt] & 1) puts("-1"), exit(0);
      for (int x = re; x; x = f[x]) w[x] -= w[rt] >> 1;
    } else {
      if (w[rt]) puts("-1"), exit(0);
      sort(b + 1, b + len + 1);
      for (int x = re; x; x = f[x]) w[x] -= w[len + 1 >> 1];
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) ans += abs(w[i]);
  printf("%lld\n", ans);
  return 0;
}
