#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chkmax(T &a, T b) {
  return (a < b) ? a = b, 1 : 0;
}
template <typename T>
bool chkmin(T &a, T b) {
  return (a > b) ? a = b, 1 : 0;
}
inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') fh = -1;
  for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ '0');
  return x * fh;
}
int n, m;
bool gi;
int he, to;
int num[100005];
vector<int> G[100005];
bool col[100005], vis[100005];
void dfs1(int x, int ff) {
  vis[x] = 1;
  for (int v : G[x]) {
    if (v == ff) continue;
    if (!vis[v]) {
      col[v] = col[x] ^ 1;
      dfs1(v, x);
    } else {
      he = x, to = v;
      if (col[x] == col[v]) gi = 1;
    }
  }
}
int num2[100005];
int sum;
int ans;
void dfs2(int x, int ff) {
  for (int v : G[x]) {
    if (v != ff && !(x == he && v == to || x == to && v == he)) {
      dfs2(v, x);
      num2[x] += num2[v];
      num[x] += num[v];
    }
  }
}
int stk[100005], top;
int main() {
  n = read();
  m = read();
  for (int i = (1), _end_ = (int)(m); i <= _end_; ++i) {
    int x = read(), y = read();
    G[x].push_back(y);
    G[y].push_back(x);
  }
  col[1] = 1;
  dfs1(1, 0);
  for (int i = (1), _end_ = (int)(n); i <= _end_; ++i) {
    if (col[i])
      num[i] = 1;
    else
      num[i] = -1;
  }
  for (int i = (1), _end_ = (int)(n); i <= _end_; ++i) sum += num[i];
  if (m == n - 1) {
    if (sum) {
      puts("-1");
      return 0;
    }
  } else {
    if (gi) {
      if (sum % 2 == 1) {
        puts("-1");
        return 0;
      }
      num[he] -= (sum / 2);
      num[to] += (sum / 2);
      ans += abs(sum / 2);
    } else {
      if (sum) {
        puts("-1");
        return 0;
      }
      num2[he] = 1;
      num2[to] = -1;
    }
  }
  dfs2(1, 0);
  for (int i = (1), _end_ = (int)(n); i <= _end_; ++i) {
    if (!num2[i])
      ans += 1ll * abs(num[i]);
    else {
      if (num2[i]) num[i] = -num[i];
      stk[++top] = num[i];
    }
  }
  stk[++top] = 0;
  sort(stk + 1, stk + top + 1);
  int X = stk[(top + 1) / 2];
  for (int i = (1), _end_ = (int)(top); i <= _end_; ++i) ans += abs(X - stk[i]);
  printf("%d\n", ans);
  return 0;
}
