#include <bits/stdc++.h>
using namespace std;
vector<int> v[100100];
int n, m, a, b, d[100100], ile[100100], sz[100100];
long long int ans = 0, parz = 0;
void dfs(int first, int par) {
  parz += d[first];
  sz[first] = 1;
  if (d[first]) ile[first]++;
  for (auto u : v[first])
    if (u != par) {
      d[u] = (d[first] ^ 1);
      dfs(u, first);
      sz[first] += sz[u];
      ile[first] += ile[u];
    }
}
inline int roz(int first, int second) { return abs(first - second); }
void zlicz(int first, int par) {
  for (auto u : v[first])
    if (u != par) {
      zlicz(u, first);
      ans += roz(ile[u], sz[u] - ile[u]);
    }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = (1); i <= (m); ++i) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  if (m == n - 1) {
    dfs(1, 1);
    if (parz != n - parz) {
      cout << -1;
      return 0;
    };
    zlicz(1, 1);
    {
      cout << ans;
      return 0;
    };
  }
}
