#include <bits/stdc++.h>
using namespace std;
const int N = 501010;
int n, m, i, a, b, f[N], rd[N], deep[N], odd[N], even[N], ans;
queue<int> A[N], B[N];
vector<int> e[N];
void dfs(int x, int fa) {
  int i;
  deep[x] = deep[fa] + 1;
  for (i = 0; i < e[x].size(); i++)
    if (e[x][i] != fa) {
      dfs(e[x][i], x);
      f[e[x][i]] = x;
      rd[x]++;
    }
}
void gao(int x, int fa) {
  int i;
  odd[x] = x;
  even[x] = x;
  if (rd[x] == 0) {
    if (deep[x] & 1)
      A[odd[x]].push(x);
    else
      B[even[x]].push(x);
  } else {
    for (i = 0; i < e[x].size(); i++)
      if (e[x][i] != fa) {
        gao(e[x][i], x);
        if (A[odd[x]].size() > A[odd[e[x][i]]].size())
          swap(odd[x], odd[e[x][i]]);
        while (!A[odd[x]].empty()) {
          int tmp = A[odd[x]].front();
          A[odd[e[x][i]]].push(tmp);
          A[odd[x]].pop();
        }
        odd[x] = odd[e[x][i]];
        if (B[even[x]].size() > B[even[e[x][i]]].size())
          swap(even[x], even[e[x][i]]);
        while (!B[even[x]].empty()) {
          B[even[e[x][i]]].push(B[even[x]].front());
          B[even[x]].pop();
        }
        even[x] = even[e[x][i]];
      }
    while ((!A[odd[x]].empty()) && (!B[even[x]].empty()) &&
           (A[odd[x]].size() + B[even[x]].size() >= 3)) {
      int a = A[odd[x]].front(), b = B[even[x]].front();
      ans = ans + ((deep[a] + deep[b] - deep[x]) * 2 - 2) / 2;
      A[odd[x]].pop();
      B[even[x]].pop();
      rd[f[a]]--;
      if (rd[f[a]] == 0) {
        if (deep[f[a]] & 1)
          A[odd[x]].push(f[a]);
        else
          B[even[x]].push(f[a]);
      }
      rd[f[b]]--;
      if (rd[f[b]] == 0) {
        if (deep[f[b]] & 1)
          A[odd[x]].push(f[b]);
        else
          B[even[x]].push(f[b]);
      }
    }
    if ((!A[odd[x]].empty()) && (!B[even[x]].empty()) &&
        (A[odd[x]].size() + B[even[x]].size() == 2)) {
      int a = A[odd[x]].front();
      A[odd[x]].pop();
      int b = B[even[x]].front();
      B[even[x]].pop();
      ans = ans + (deep[a] + deep[b] - deep[x]) / 2;
      rd[f[x]]--;
    }
    if (A[odd[x]].size() + B[even[x]].size() == 1) {
      int tmp;
      if (A[odd[x]].size())
        tmp = A[odd[x]].front(), A[odd[x]].pop();
      else
        tmp = B[even[x]].front(), B[even[x]].pop();
      ans = ans + (deep[tmp] - deep[x]) / 2;
      if (deep[tmp] % 2 == deep[x] % 2) {
        if (deep[x] % 2 == 0)
          B[even[x]].push(x);
        else
          A[odd[x]].push(x);
      } else
        rd[f[x]]--;
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &a, &b);
    e[a].push_back(b);
    e[b].push_back(a);
  }
  dfs(1, 0);
  gao(1, 0);
  if (A[odd[1]].size() + B[even[1]].size()) ans = -1;
  printf("%d\n", ans);
}
