#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
const long long N = 200000 + 10;
const long long MOD = 1000000000 + 7;
const long long INF = 1000000010;
const long long LOG = 25;
long long n, m, E, O, h[N], par[N], vaz[N], ans, Ev[N], Od[N], mark[N], a[N],
    sz[N], cnt[N];
vector<long long> G[N];
bool f = 0;
vector<long long> pth;
void DFS(long long v = 1, long long p = 0) {
  mark[v] = 1;
  Ev[v]++;
  for (auto u : G[v]) {
    if (mark[u]) continue;
    h[u] = h[v] + 1;
    par[u] = v;
    DFS(u, v);
    Ev[v] += Od[u];
    Od[v] += Ev[u];
  }
  if (p != 0) ans += abs(Ev[v] - Od[v]);
}
void DFS2(long long v = 1, long long p = 0) {
  mark[v] = 1;
  par[v] = p;
  for (auto u : G[v]) {
    if (u == p) continue;
    if (mark[u] == 0) {
      h[u] = h[v] + 1;
      DFS2(u, v);
    } else if (h[u] < h[v]) {
      if (h[u] % 2 == h[v] % 2) f = 1;
      long long nd = v;
      while (nd != par[u]) {
        pth.push_back(nd);
        nd = par[nd];
      }
    }
  }
}
void DFS3(long long v, long long p = 0) {
  mark[v] = 1;
  if (h[v] % 2 == f)
    cnt[v]++;
  else
    sz[v]++;
  for (auto u : G[v]) {
    if (!mark[u]) {
      h[u] = h[v] + 1;
      DFS3(u, v), sz[v] += sz[u], cnt[v] += cnt[u];
    }
  }
  ans += abs(sz[v] - cnt[v]);
}
long long Solve(long long x) {
  long long Last = x;
  long long res = abs(x);
  for (long long i = 0; i < n - 1; i++) {
    Last = Last - a[i];
    res += abs(Last);
  }
  return res;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  long long nn = n;
  for (long long i = 0; i < m; i++) {
    long long v, u;
    cin >> v >> u;
    G[v].push_back(u);
    G[u].push_back(v);
  }
  if (m == n) {
    DFS2(1);
    memset(mark, 0, sizeof mark);
    if (!f) {
      n = pth.size();
      for (auto u : pth) mark[u] = 1;
      for (long long i = 0; i < pth.size(); i++) {
        DFS(pth[i]);
        if (i % 2 == 0)
          E += Ev[pth[i]], a[i] = Ev[pth[i]] - Od[pth[i]];
        else
          a[i] = Od[pth[i]] - Ev[pth[i]], E += Od[pth[i]];
      }
      if ((nn % 2 == 1) || (E != nn / 2)) return cout << -1, 0;
      long long l = -nn - 1, r = nn + 1;
      while (r - l > 1) {
        long long md = (l + r) >> 1;
        if (Solve(md + 1) >= Solve(md))
          r = md;
        else
          l = md;
      }
      cout << ans + Solve(r) << '\n';
      return 0;
    }
    assert(0);
    long long root = 1;
    DFS(1);
    if (Ev[1] % 2 != Od[1] % 2) return cout << -1, 0;
    if (h[pth[0]] % 2 == 0) {
      if (Ev[1] > Od[1])
        root = G[1][0], f = 1;
      else
        f = 0;
    } else {
      if (Od[1] > Ev[1])
        root = G[1][0], f = 0;
      else
        f = 1;
    }
    ans = 0;
    ans += abs(Ev[1] - Od[1]) / 2;
    cnt[pth[0]] += (abs(Ev[1] - Od[1]) / 2);
    cnt[pth.back()] += (abs(Ev[1] - Od[1]) / 2);
    memset(mark, 0, sizeof mark);
    memset(Ev, 0, sizeof Ev);
    memset(Od, 0, sizeof Od);
    DFS3(root);
    cout << ans << '\n';
    return 0;
  }
  DFS(1);
  if (Ev[1] != Od[1]) return cout << -1, 0;
  cout << ans;
  return 0;
}
