#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int MAX_N = 100005;
typedef long long i64;

class Edge {
public:
  int nxt, to;
} e[MAX_N << 1];

int head[MAX_N], cnt;
void addedge(int u, int v) {
  e[++cnt] = (Edge){head[u], v}, head[u] = cnt;
  e[++cnt] = (Edge){head[v], u}, head[v] = cnt;
}

int N, M, dep[MAX_N], fa[MAX_N], sum[MAX_N];
int c1, c2;
bool flag;

void dfs(int u, int v) {
  dep[u] = dep[v] + 1, fa[u] = v;
  if (dep[u] & 1) {
    sum[u] = 1;
  } else {
    sum[u] = -1;
  }

  for (int i = head[u]; i; i = e[i].nxt)
    if (!dep[e[i].to]) {
      dfs(e[i].to, u);
      sum[u] += sum[e[i].to];
    } else if (e[i].to != v && dep[e[i].to] < dep[u]) {
      if (!(dep[u] - dep[e[i].to] & 1)) flag = 1;
      c1 = u, c2 = e[i].to;
    }
}

int main() {
  scanf("%d%d", &N, &M);
  for (int i = 1, u, v; i <= M; ++i) {
    scanf("%d%d", &u, &v);
    addedge(u, v);
  }

  dfs(1, 0);

  if (M == N - 1) {
    if (sum[1] != 0) {
      puts("-1");
      return 0;
    }
    
    i64 res = 0;
    for (int i = 2; i <= N; ++i)
      res += abs(sum[i]);
    printf("%lld\n", res);
  } else if (flag) {
    if (sum[1] & 1) {
      puts("-1");
      return 0;
    }

    int add = -sum[1] / 2;
    while (c1) sum[c1] += add, c1 = fa[c1];
    while (c2) sum[c2] += add, c2 = fa[c2];
    
    i64 res = abs(add);
    for (int i = 2; i <= N; ++i)
      res += abs(sum[i]);
    printf("%lld\n", res);
  } else {
    if (sum[1] != 0) {
      puts("-1");
      return 0;
    }

    static int A[MAX_N];
    static bool vis[MAX_N];
    
    int tot = 1;

    while (c1 != c2) vis[c1] = 1, A[++tot] = sum[c1], c1 = fa[c1];
    
    sort(A + 1, A + tot + 1);
    i64 res = 0;
    for (int i = 2; i <= N; ++i)
      if (!vis[i]) res += abs(sum[i]);
    
    for (int i = 1; i <= tot / 2; ++i)
      res += A[tot - i + 1] - A[i];
    printf("%lld\n", res);
  }
  
  return 0;
}
