#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct edge {
  int nxt, to;
} e[N << 1];
int fir[N], cnt = 0;
int n, m, w[N], sum[N];
inline void addedge(int x, int y) {
  e[++cnt] = (edge){fir[x], y};
  fir[x] = cnt;
}
namespace Tree {
inline void Dfs(int x, int f) {
  if (x == 1)
    w[x] = 1;
  else
    w[x] = -w[f];
  sum[x] = w[x];
  for (int i = fir[x]; i; i = e[i].nxt)
    if (e[i].to != f) {
      Dfs(e[i].to, x);
      sum[x] += sum[e[i].to];
    }
}
inline void solve() {
  int jud = 0;
  long long ans = 0;
  Dfs(1, 0);
  for (int i = (1); i <= (n); i++) {
    ans += abs(sum[i]);
    jud += w[i];
  }
  if (jud)
    puts("-1");
  else
    printf("%lld\n", ans);
}
}  // namespace Tree
namespace Ring {
inline void solve() {}
}  // namespace Ring
int main() {
  scanf("%d%d", &n, &m);
  int x, y;
  for (int i = (1); i <= (m); i++) {
    scanf("%d%d", &x, &y);
    addedge(x, y);
    addedge(y, x);
  }
  if (m == n - 1)
    Tree ::solve();
  else
    Ring ::solve();
  return 0;
}
