// Copyright (C) 2017 __debug.

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; version 3

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; If not, see <http://www.gnu.org/licenses/>.


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>

#define x first
#define y second
#define MP std::make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

using std::pair;
using std::vector;
using std::string;

typedef long long LL;
typedef pair<int, int> Pii;

const int oo = 0x3f3f3f3f;

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, true : false; }
string procStatus()
{
    std::ifstream t("/proc/self/status");
    return string(std::istreambuf_iterator<char>(t), std::istreambuf_iterator<char>());
}
template<typename T> T read(T &x)
{
    int f = 1;
    char ch = getchar();
    for (; !isdigit(ch); ch = getchar())
        f = (ch == '-' ? -1 : 1);
    for (x = 0; isdigit(ch); ch = getchar())
        x = 10 * x + ch - '0';
    return x *= f;
}
template<typename T> void write(T x)
{
    if (x == 0) {
        putchar('0');
        return;
    }
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    static char s[20];
    int top = 0;
    for (; x; x /= 10)
        s[++top] = x % 10 + '0';
    while (top)
        putchar(s[top--]);
}
// EOT

const int MAXN = 1e5 + 5;

struct Edge {
    int v, next;
    Edge() { }
    Edge(int v_, int next_): v(v_), next(next_) { }
};

int N, M;
int tote, head[MAXN];
Edge edge[MAXN * 2 + 5];

inline void addEdge(int u, int v)
{
    edge[++tote] = Edge(v, head[u]);
    head[u] = tote;
}

void input()
{
    read(N); read(M);
    tote = 1;
    for (int i = 1; i <= M; ++i) {
        int u, v;
        read(u); read(v);
        addEdge(u, v);
        addEdge(v, u);
    }
}

int dep[MAXN], fa[MAXN], size[MAXN], tot[MAXN];
int S, T;

void dfs(int u)
{
    dep[u] = dep[fa[u]] + 1;
    size[u] = 1;
    tot[u] = dep[u] & 1;
    for (int i = head[u]; i; i = edge[i].next) {
        int v = edge[i].v;
        if (v == fa[u]) continue;
        if (dep[v]) {
            S = u;
            T = v;
        } else {
            fa[v] = u;
            dfs(v);
            size[u] += size[v];
            tot[u] += tot[v];
        }
    }
}

void solve()
{
    dfs(1);

    if (!S) {                   // tree
        if (size[1] - tot[1] * 2 != 0) {
            puts("-1");
            return;
        }

        LL ans = 0;
        for (int u = 1; u <= N; ++u) {
            ans += std::abs(size[u] - tot[u] * 2);
            // printf("%d: contrib %d\n", u, std::abs(size[u] - tot[u] * 2));
        }
        printf("%lld\n", ans);

    } else {
        memset(fa, 0, sizeof(fa));
        memset(dep, 0, sizeof(dep));
        int S0 = S;
        dfs(S);
        if (S != S0)
            std::swap(S, T);
        assert(S == S0);

        if (dep[S] % 2 == dep[T] % 2) { // odd cycle
            if ((size[S] - tot[S] * 2) % 2 != 0) {
                puts("-1");
                return;
            }

            // printf("S=%d, T=%d\n", S, T);
            int d = (size[S] - tot[S] * 2) / 2;
            LL ans = abs(d);
            for (int u = 1; u <= N; ++u) {
                if (u != S)
                    ans += abs(size[u] - tot[u] * 2);
            }
            for (int u = T; u != S; u = fa[u]) {
                ans -= abs(size[u] - tot[u] * 2);
                ans += abs(size[u] - tot[u] * 2 - d);
            }

            printf("%lld\n", ans);

        } else {                    // even cycle
            if (size[S] - tot[S] * 2 != 0) {
                puts("-1");
                return;
            }

            vector<int> list;
            list.push_back(0);
            for (int u = T; u != S; u = fa[u]) {
                // printf("%d: %d\n", u, size[u] - tot[u] * 2);
                list.push_back(size[u] - tot[u] * 2);
            }
            std::sort(ALL(list));
            int x = list[SZ(list) / 2];
            // printf("S=%d, T=%d, x=%d\n", S, T, x);

            LL ans = abs(x);
            for (int u = 1; u <= N; ++u) {
                ans += abs(size[u] - tot[u] * 2);
            }
            for (int u = T; u != S; u = fa[u]) {
                ans -= abs(size[u] - tot[u] * 2);
                ans += abs(size[u] - tot[u] * 2 - x);
            }
            printf("%lld\n", ans);
        }
    }
}

int main()
{
#ifdef __DEBUG
    freopen("F.in", "r", stdin);
    freopen("F.out", "w", stdout);
#endif

    input();
    solve();

    return 0;
}

// 子曰：“学而不思则罔，思而不学则殆。”
// --论语，为政篇


Measured memory_bytes: 9084