#include <bits/stdc++.h>
using namespace std;
clock_t __stt;
inline void TStart() { __stt = clock(); }
inline void TReport() {
  printf("\nTaken Time : %.3lf sec\n",
         (double)(clock() - __stt) / CLOCKS_PER_SEC);
}
template <typename T>
T MIN(T a, T b) {
  return a < b ? a : b;
}
template <typename T>
T MAX(T a, T b) {
  return a > b ? a : b;
}
template <typename T>
T ABS(T a) {
  return a > 0 ? a : (-a);
}
template <typename T>
void UMIN(T &a, T b) {
  if (b < a) a = b;
}
template <typename T>
void UMAX(T &a, T b) {
  if (b > a) a = b;
}
int n, m, v1 = -1, v2 = -1, col[100005], cnt[100005], sum[2], res = 0;
vector<int> adj[100005], vs(1, 0);
bool vis[100005], is_even_loop;
void dfs_loop(int ver, int f) {
  int i, j, k;
  sort(adj[ver].begin(), adj[ver].end());
  for (i = 0; i < (int)adj[ver].size(); ++i) {
    if (adj[ver][i] != f) {
      if (vis[adj[ver][i]]) {
        v1 = adj[ver][i];
        v2 = ver;
      } else {
        vis[adj[ver][i]] = 1;
        dfs_loop(adj[ver][i], ver);
      }
    }
  }
}
void dfs_col(int ver) {
  int i, j, k;
  if (col[ver])
    cnt[ver] = 1;
  else
    cnt[ver] = -1;
  ++sum[col[ver]];
  for (i = 0; i < (int)adj[ver].size() && is_even_loop; ++i) {
    if (col[adj[ver][i]] == -1) {
      col[adj[ver][i]] = 1 - col[ver];
      dfs_col(adj[ver][i]);
    } else if (col[adj[ver][i]] != 1 - col[ver]) {
      is_even_loop = 0;
    }
  }
}
void solve_tree(int ver, int f) {
  int i, j, k;
  for (i = 0; i < (int)adj[ver].size(); ++i) {
    if (adj[ver][i] != f) {
      solve_tree(adj[ver][i], ver);
      cnt[ver] += cnt[adj[ver][i]];
    }
  }
  res += ABS(cnt[ver]);
}
int solve_odd(int ver, int f) {
  int i, j, k, ret = 0;
  for (i = 0; i < (int)adj[ver].size(); ++i) {
    if (adj[ver][i] != f) {
      ret ^= solve_odd(adj[ver][i], ver);
      cnt[ver] += cnt[adj[ver][i]];
    }
  }
  if (ver == v1 || ver == v2) ret ^= 1;
  if (!ret) {
    res += ABS(cnt[ver]);
  } else if (col[v2]) {
    vs.push_back(-cnt[ver]);
  } else {
    vs.push_back(cnt[ver]);
  }
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  int i, j, k;
  for (i = 0; i < m; ++i) {
    scanf("%d%d", &j, &k);
    adj[--j].push_back(--k);
    adj[k].push_back(j);
  }
  memset(col, -1, sizeof(col));
  col[0] = 1;
  is_even_loop = 1;
  dfs_col(0);
  if (m == n - 1) {
    if (sum[0] != sum[1]) {
      printf("-1\n");
      return 0;
    }
    solve_tree(0, -1);
  } else {
    vis[0] = 1;
    dfs_loop(0, -1);
    adj[v1].erase(lower_bound(adj[v1].begin(), adj[v1].end(), v2));
    adj[v2].erase(lower_bound(adj[v2].begin(), adj[v2].end(), v1));
    if (is_even_loop) {
      solve_odd(0, -1);
      sort(vs.begin(), vs.end());
      int cv = vs[vs.size() / 2];
      for (i = 0; i < (int)vs.size(); ++i) {
        res += ABS(cv - vs[i]);
      }
    } else {
      memset(col, -1, sizeof(col));
      col[0] = 1;
      is_even_loop = 1;
      sum[0] = sum[1] = 0;
      memset(cnt, 0, sizeof(cnt));
      dfs_col(0);
      if (ABS(sum[0] - sum[1]) & 1) {
        printf("-1\n");
        return 0;
      }
      if (col[v1]) {
        cnt[v1] += (sum[0] - sum[1]) / 2;
        cnt[v2] += (sum[0] - sum[1]) / 2;
      } else {
        cnt[v1] -= (sum[0] - sum[1]) / 2;
        cnt[v2] -= (sum[0] - sum[1]) / 2;
      }
      res = ABS(sum[0] - sum[1]) / 2;
      solve_tree(0, -1);
    }
  }
  printf("%d\n", res);
  return 0;
}
