#include <bits/stdc++.h>
using namespace std;
int IN() {
  int c, f, x;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  c == '-' ? (f = 1, x = 0) : (f = 0, x = c - '0');
  while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + c - '0';
  return !f ? x : -x;
}
const int N = 100000 + 19;
struct Edge {
  int y, nxt;
} E[N * 2];
int las[N], fa[N], val[N], col[N], sum[N], sz[N], tar[N], coef[N];
int n, cnt, x, y, u, v, m, ji, ou;
vector<int> V;
long long ans;
void Link(int x, int y) {
  E[cnt] = (Edge){y, las[x]};
  las[x] = cnt++;
  E[cnt] = (Edge){x, las[y]};
  las[y] = cnt++;
}
int getf(int x) {
  if (fa[x] == x) return x;
  int f = fa[x];
  fa[x] = getf(fa[x]);
  val[x] ^= val[f];
  return fa[x];
}
void Union(int x, int y) {
  int fx = getf(x), fy = getf(y);
  fa[fx] = fy;
  val[fx] = 1 ^ val[x] ^ val[y];
}
void dfs(int x, int fa) {
  sum[x] = col[x];
  sz[x] = 1;
  for (int i = las[x], y; ~i; i = E[i].nxt)
    if ((y = E[i].y) != fa) {
      col[y] = col[x] ^ 1;
      dfs(y, x);
      sum[x] += sum[y];
      sz[x] += sz[y];
    }
  tar[x] = sz[x] - sum[x];
}
void Dfs(int x, int fa) {
  sum[x] = col[x];
  for (int i = las[x], y; ~i; i = E[i].nxt)
    if ((y = E[i].y) != fa) {
      Dfs(y, x);
      sum[x] += sum[y];
      ans += abs(sum[y] - tar[y]);
    }
}
void Dfs2(int x, int fa) {
  for (int i = las[x], y; ~i; i = E[i].nxt)
    if ((y = E[i].y) != fa) {
      Dfs2(y, x);
      coef[x] += coef[y];
      if (coef[y] == 0) {
        ans += abs(sum[y] - tar[y]);
      } else {
        V.push_back(coef[y] == 1 ? tar[y] - sum[y] : sum[y] - tar[y]);
      }
    }
}
void Work1() {
  if (n % 2 == 1 || sum[1] != n / 2) {
    puts("-1");
    exit(0);
  }
  Dfs(1, -1);
}
void Work2() {
  coef[u] = 1;
  coef[v] = -1;
  Dfs2(1, -1);
  V.push_back(0);
  sort(V.begin(), V.end());
  for (int i = 0; i < V.size(); i++) ans += abs(V[i] - V[int(V.size()) / 2]);
}
void Work3() {
  if ((n - sum[1] - sum[1]) % 2 != 0) {
    puts("-1");
    exit(0);
  }
  int tmp = (n - sum[1] - sum[1]) / 2;
  col[u] += tmp;
  col[v] += tmp;
  Dfs(1, -1);
  ans += abs(tmp);
}
int main() {
  memset(las, -1, sizeof(las));
  n = IN(), m = IN();
  for (int i = 1; i < n + 1; i++) fa[i] = i, val[i] = 0;
  for (int i = 0; i < m; i++) {
    x = IN(), y = IN();
    if (getf(x) != getf(y)) {
      Union(x, y);
      Link(x, y);
    } else {
      u = x, v = y;
      if ((val[x] ^ val[y]) == 0)
        ji = 1;
      else
        ou = 1;
    }
  }
  dfs(1, -1);
  if (m == n - 1) Work1();
  if (m == n && ou) Work2();
  if (m == n && ji) Work3();
  cout << ans << endl;
}
