#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const long long LINF = INF * (long long)INF;
const int MAX = 1000 * 100 + 47;
vector<int> g[MAX];
int C[MAX];
int a = -1;
int b = -1;
bool isLoop;
bool odd;
int VAL[MAX];
int n, m;
void dfs(int v, int c, int p) {
  C[v] = c;
  for (int i = (0); i < ((int)g[v].size()); i++) {
    int to = g[v][i];
    if (to == p) continue;
    if (C[to] != -1) {
      if (a == -1) {
        isLoop = true;
        if (C[to] != c ^ 1) odd = true;
        a = v;
        b = to;
      }
      continue;
    }
    dfs(to, c ^ 1, v);
  }
}
void imp() {
  cout << -1 << endl;
  exit(0);
}
void rem(int a, int b) {
  int ind = -1;
  for (int i = (0); i < ((int)g[a].size()); i++) {
    if (g[a][i] == b) {
      ind = i;
      break;
    }
  }
  g[a].erase(g[a].begin() + ind);
}
long long ans = 0;
long long bal = 0;
void dfs1(int v, int p) {
  for (int i = (0); i < ((int)g[v].size()); i++) {
    int to = g[v][i];
    if (to != p) dfs1(to, v);
  }
  bal += VAL[v];
  ans += abs(bal);
}
long long f(int c) {
  ans = 0;
  bal = 0;
  VAL[a] += c;
  VAL[b] -= c;
  dfs1(0, 0);
  VAL[a] -= c;
  VAL[b] += c;
  return ans + abs(c);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = (0); i < (m); i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  if (n % 2) imp();
  memset(C, -1, sizeof(C));
  dfs(0, 0, 0);
  int cnt = 0;
  for (int i = (0); i < (n); i++) {
    cnt += C[i];
    VAL[i] = C[i] == 0 ? -1 : 1;
  }
  if (!isLoop) {
    if (cnt * 2 != n) imp();
    ans = 0;
    bal = 0;
    dfs1(0, 0);
    cout << ans << endl;
    return 0;
  }
  rem(a, b);
  rem(b, a);
  if (odd) {
    int val = (n - 2 * cnt) / 2;
    VAL[a] += val;
    VAL[b] += val;
    ans = 0;
    bal = 0;
    dfs1(0, 0);
    cout << ans + abs(val) << endl;
    return 0;
  }
  cout << f(-1) << endl;
  return 0;
  if (cnt * 2 != n) imp();
  int l = -n, r = n;
  while (r - l > 2) {
    int l1 = l + (r - l) / 3;
    int r1 = l + 2 * (r - l) / 3;
    if (f(l1) > f(r1)) {
      l = l1;
    } else {
      r = r1;
    }
  }
  long long ans = min(f(l), min(f(l + 1), f(l + 2)));
  cout << ans << endl;
}
