#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int N, M;
vector<int> adj[MAXN];
int dep[MAXN], sum[MAXN], k[MAXN];
bool vis[MAXN];
int cU, cV, clen;
vector<int> num;
void dfs(int u, int fa = 0) {
  vis[u] = true;
  for (auto v : adj[u]) {
    if (v == fa) continue;
    if (vis[v]) {
      cU = u;
      cV = v;
      clen = dep[u] - dep[v] + 1;
      continue;
    }
    dep[v] = dep[u] + 1;
    dfs(v, u);
  }
}
void Calc(int u, int fa = 0) {
  vis[u] = true;
  for (auto v : adj[u]) {
    if (v == fa || vis[v]) continue;
    Calc(v, u);
    sum[u] += sum[v];
    k[u] += k[v];
  }
}
int main() {
  freopen("print.in", "r", stdin);
  freopen("print.out", "w", stdout);
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= M; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(1);
  int val = 0;
  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    sum[i] = (dep[i] & 1) ? -1 : 1;
    val += sum[i];
  }
  if (M == N - 1) {
    if (val != 0) {
      puts("-1");
      return 0;
    }
  } else {
    if (clen & 1) {
      if (val & 1) {
        puts("-1");
        return 0;
      } else {
        ans += (abs(val) / 2);
        sum[cU] -= val / 2;
        sum[cV] -= val / 2;
      }
    } else {
      if (val != 0) {
        puts("-1");
        return 0;
      } else {
        k[cU] = 1;
        k[cU] = -1;
      }
    }
  }
  memset(vis, 0, sizeof vis);
  Calc(1);
  for (int i = 1; i <= N; i++) {
    if (k[i] == 0)
      ans += abs(sum[i]);
    else {
      if (k[i] == 1) sum[i] = -sum[i];
      num.push_back(sum[i]);
    }
  }
  num.push_back(0);
  sort(num.begin(), num.end());
  int x = num[num.size() / 2];
  for (auto s : num) ans += 1LL * abs(s - x);
  printf("%lld\n", ans);
  fclose(stdin);
  fclose(stdout);
  return 0;
}
