#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)(1e5) + 5;
vector<int> nxt[maxn];
int fa[maxn], dfn[maxn], siz[maxn], su[maxn];
int ci[maxn], cc[maxn], va[maxn];
int cnt, idx;
inline int iabs(int x) { return x >= 0 ? x : -x; }
void dfs(int x, int f, int v) {
  fa[x] = f;
  dfn[x] = ++idx;
  siz[x] = 1;
  su[x] = v;
  v ^= 1;
  for (int i = 0; i < (int)nxt[x].size(); ++i)
    if (nxt[x][i] != f) {
      if (!dfn[nxt[x][i]]) {
        dfs(nxt[x][i], x, v);
        su[x] += su[nxt[x][i]];
        siz[x] += siz[nxt[x][i]];
      } else if (dfn[nxt[x][i]] < dfn[x]) {
        ci[cnt = 1] = x;
        cc[x] = 1;
        int c = x, a = nxt[x][i];
        while (c != a) {
          c = fa[c];
          ci[++cnt] = c;
          cc[c] = 1;
        }
      }
    }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    nxt[u].push_back(v);
    nxt[v].push_back(u);
  }
  dfs(1, 0, 0);
  if (!cnt || !(cnt & 1)) {
    if (2 * su[1] != siz[1])
      printf("-1\n");
    else {
      int ret = 0;
      for (int i = 1; i <= n; ++i)
        if (!cc[i]) ret += iabs(2 * su[i] - siz[i]);
      if (cnt) {
        for (int i = 1; i <= cnt; ++i) va[i] = 2 * su[i] - siz[i];
        sort(va + 1, va + cnt + 1);
        for (int i = 1; i <= cnt; ++i)
          ret += iabs(va[(cnt + 1) / 2] - 2 * su[i] + siz[i]);
      }
      printf("%d\n", ret);
    }
  } else {
    if ((2 * su[1] - siz[1]) & 1)
      printf("-1\n");
    else {
    }
  }
  return 0;
}
