#include <bits/stdc++.h>
using namespace std;
template <class T, class T2>
inline void chkmax(T &x, const T2 &y) {
  if (x < y) x = y;
}
template <class T, class T2>
inline void chkmin(T &x, const T2 &y) {
  if (x > y) x = y;
}
const int MAXN = (1 << 20);
int n, m;
vector<int> adj[MAXN];
void read() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[v].push_back(u);
    adj[u].push_back(v);
  }
}
int col[MAXN];
int cnt[MAXN];
int tr_sz[MAXN];
void dfs(int u, int pr, int c) {
  col[u] = c;
  cnt[u] = col[u];
  tr_sz[u] = 1;
  for (int v : adj[u])
    if (v != pr) {
      dfs(v, u, c ^ 1);
      cnt[u] += cnt[v];
      tr_sz[u] += tr_sz[v];
    }
}
int64_t answer;
void dfs_solve(int u, int pr) {
  if (pr != -1) answer += abs(tr_sz[u] - 2 * cnt[u]);
  for (int v : adj[u])
    if (v != pr) dfs_solve(v, u);
}
void solve() {
  dfs(1, 1, 0);
  dfs_solve(1, -1);
  if (tr_sz[1] - 2 * cnt[1]) answer = -1;
  cout << answer << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  read();
  solve();
  return 0;
}
