#include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
#include <bitset>
#include <queue>
#include <stack>
#include <deque>
#include <complex>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <random>
#include <functional>

#define int long long

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

using std::map;
using std::set;
using std::bitset;
using std::vector;
using std::string;
using std::multimap;
using std::multiset;
using std::deque;
using std::queue;
using std::stack;
using std::pair;
using std::iterator;

using std::sort;
using std::stable_sort;
using std::reverse;
using std::max_element;
using std::min_element;
using std::unique;
using std::ios_base;
using std::swap;
using std::fill;

using std::setprecision;
using std::fixed;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;

ll min(ll a, ll b) {return a < b ? a : b;}
//ll min(int a, ll b) {return a < b ? a : b;}
//ll min(ll a, int b) {return a < b ? a : b;}
//ll min(int a, int b) {return a < b ? a : b;}

ll max(ll a, ll b) {return a > b ? a : b;}
//ll max(int a, ll b) {return a > b ? a : b;}
//ll max(ll a, int b) {return a > b ? a : b;}
//ll max(int a, int b) {return a > b ? a : b;}

namespace MySpace{

};

#define F(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()

int n, m;
vector<int> graph[500000];

int C = -1, D = -1;
int c[500000];
int ss[500000];
int s[500000];
int pre[500000];
int dep[500000];

void color(int v, int p)
{
    if (c[v] != -1) {C = v, D = p; return;}
    if (p == -1) c[v] = 0;
    else c[v] = 1 ^ c[p];
    for (auto u : graph[v]) if (u != p) color(u, v);
}

void dfs(int v, int p)
{
    ss[v] = c[v];
    s[v] = 1;
    for (auto u : graph[v]) if (u != p) if (v == C && u == D) continue; else if (v == D && u == C) continue; else dfs(u, v), ss[v] += ss[u], s[v] += s[u];
}

void dist(int v, int p)
{
    if (p == -1) dep[v] = 0;
    else dep[v] = dep[p] + 1;
    pre[v] = p;
    for (auto u : graph[v]) if (u != p) if (v == C && u == D) continue; else if (v == D && u == C) continue; else dist(u, v);
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int v, u;
        cin >> v >> u, v--, u--;
        graph[v].push_back(u);
        graph[u].push_back(v);
    }
    fill(c, c + n, -1);
    if (m + 1 == n)
    {
        color(0, -1);
        int cnt = 0;
        for (int i = 0; i < n; i++) cnt += c[i];
        if (cnt * 2 != n) cout << -1, exit(0);
        dfs(0, -1);
        int ans = 0;
        for (int i = 0; i < n; i++) ans += abs(s[i] - 2 * ss[i]);
        cout << ans;
        return 0;
    }
    color(0, -1);
    int A = -1, B = -1;
    for (int i = 0; i < n; i++)
    {
        for (auto it : graph[i])
        {
            if (c[i] == c[it])
            {
                A = i, B = it;
            }
        }
    }
    if (A == -1)
    {
        int cnt = 0;
        for (int i = 0; i < n; i++) cnt += c[i];
        if (cnt * 2 != n) cout << -1, exit(0);
        dist(0, -1);
        vector<int> s1(n), s2(n);
        int ans = 0;
        dfs(0, -1);
        for (int i = 0; i < n; i++) s1[i] = ss[i], s2[i] = s[i] - ss[i];
        vector<int> si(n);
        while (dep[C] > dep[D]) si[C] = -1, C = pre[C];
        while (dep[C] < dep[D]) si[D] = 1, D = pre[D];
        while (C != D) si[C] = -1, si[D] = 1, C = pre[C], D = pre[D];
        //for (int i = 0; i < n; i++) cout << si[i] << " ";
        //cout << endl;
        for (int i = 0; i < n; i++) if (si[i] == 0) ans += abs(s1[i] - s2[i]);
        vector<int> pnts;
        for (int i = 0; i < n; i++) if (si[i] == -1) pnts.push_back(s1[i] - s2[i]);
        for (int i = 0; i < n; i++) if (si[i] == 1) pnts.push_back(s2[i] - s1[i]);
        sort(all(pnts));
        int x = pnts[pnts.size() / 2];
        for (int i = 0; i < n; i++) if (si[i] == -1) ans = ans + abs(s1[i] - s2[i] - x);
        for (int i = 0; i < n; i++) if (si[i] == 1) ans = ans + abs(s1[i] - s2[i] + x);
        cout << ans + abs(x);
    }
    else
    {
        int cnt = 0;
        for (int i = 0; i < n; i++) cnt += c[i];
        if (cnt % 2 != (n - cnt) % 2) cout << -1, exit(0);
        if (cnt > n / 2)
        {
            for (int i = 0; i < n; i++) c[i] ^= 1;
            cnt = n - cnt;
        }
        int ans = n / 2 - cnt;
        C = A, D = B;
        vector<int> s1(n), s2(n);
        dfs(0, -1);
        for (int i = 0; i < n; i++) s2[i] = s[i] - ss[i];
        c[A] += ans;
        c[B] += ans;
        dfs(0, -1);
        for (int i = 0; i < n; i++) s1[i] = ss[i];
        for (int i = 0; i < n; i++) ans += abs(s1[i] - s2[i]);
        cout << ans;
    }
}


Measured memory_bytes: 41592