#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
template <class T>
inline void umin(T &x, T y) {
  x = x < y ? x : y;
}
template <class T>
inline void umax(T &x, T y) {
  x = x > y ? x : y;
}
inline unsigned int R() {
  static unsigned int seed = 416;
  return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 233333;
vector<int> e[N];
int sz[N], n, m;
long long res;
void dfs(int u, int fa, int d) {
  sz[u] = d;
  for (register int i = (0); i <= (((int)e[u].size()) - 1); i++)
    if (e[u][i] != fa) dfs(e[u][i], u, -d), sz[u] += sz[e[u][i]];
  res += sz[u] > 0 ? sz[u] : -sz[u];
}
int main() {
  read(n);
  read(m);
  for (register int i = (1); i <= (m); i++) {
    int u, v;
    read(u), read(v), e[u].push_back(v), e[v].push_back(u);
  }
  if (n == m) return 0;
  dfs(1, 0, 1);
  if (sz[1])
    puts("-1");
  else
    printf("%lld", res);
  return 0;
}
