#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return x;
}
int n, m;
vector<int> e[N];
namespace Tree {
long long ans = 0;
int v[N], c[N];
void solveT(int x, int pre, int d) {
  c[x] = v[x] = d;
  for (auto y : e[x])
    if (y != pre) solveT(y, x, -d), v[x] += v[y];
  ans += abs(v[x]);
}
void solve() {
  solveT(1, 0, 1);
  printf("%lld\n", v[1] == 0 ? ans : -1);
}
}  // namespace Tree
int main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++) e[i].clear();
  for (int i = 1; i <= m; i++) {
    int a = read(), b = read();
    e[a].push_back(b);
    e[b].push_back(a);
  }
  if (m == n - 1) return (Tree ::solve()), 0;
  return 0;
}
