#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5e5;
int f[N],fst[N],nxt[N],to[N],vis[N],col[N],stack[N],top,root=1,rcnt,ring[N],n,m,mm,tmp[N];
int abs(int x){return x>0?x:-x;}
void dfs(int u,int fa)
{
    f[u]=col[u]?1:-1;
    for(int i=fst[u];i;i=nxt[i])
    {
        int v=to[i];if(v==fa)continue;
        if((u==ring[1]&&v==ring[rcnt])||(u==ring[rcnt]&&v==ring[1]))continue;
        dfs(v,u);f[u]+=f[v];
    }
}
void prework(int u,int c,int fa)
{
    vis[u]=1,col[u]=c;stack[++top]=u;
    for(int i=fst[u];i;i=nxt[i])
    {
        int v=to[i];if(v==fa)continue;
        if(u==ring[rcnt]&&v==ring[1])continue;
        if(vis[v])
        {
            while(stack[top+1]!=v)
                ring[++rcnt]=stack[top--];
            top+=rcnt;
            root=u;
            continue;
        }
        prework(v,c^1,u);
    }
    --top;
}
void ade(int u,int v){to[++mm]=v,nxt[mm]=fst[u],fst[u]=mm;}
int main()
{
    scanf("%d%d",&n,&m);if(n&1){puts("-1");return 0;}
    for(int i=1,u,v;i<=m;i++)scanf("%d%d",&u,&v),ade(u,v),ade(v,u);
    prework(1,0,0);
    dfs(root,0);
    if(m==n)
    {
        if(rcnt&1)
        {
            
            if(f[root]&1){puts("-1");return 0;}
            int t=f[root]>>1;
            for(int i=1;i<=rcnt;i++)f[ring[i]]-=t;
        }
        else
        {
            if(f[root]){puts("-1");return 0;}
            for(int i=2;i<=rcnt;i++)tmp[i]=f[ring[i]];
            tmp[1]=0;
            sort(tmp+1,tmp+rcnt+1);
            int t=tmp[rcnt>>1];
            for(int i=1;i<=rcnt;i++)f[ring[i]]-=t;
        }
    }
    else
        if(f[root]){puts("-1");return 0;}
    long long ans=0;
    for(int i=1;i<=n;i++)ans+=abs(f[i]);
    cout<<ans<<endl;
}
### Prompt

Please formulate a cpp solution to the following problem:

You are given an undirected graph with N vertices and M edges. Here, N-1≤M≤N holds and the graph is connected. There are no self-loops or multiple edges in this graph.

The vertices are numbered 1 through N, and the edges are numbered 1 through M. Edge i connects vertices a_i and b_i.

The color of each vertex can be either white or black. Initially, all the vertices are white. Snuke is trying to turn all the vertices black by performing the following operation some number of times:

* Select a pair of adjacent vertices with the same color, and invert the colors of those vertices. That is, if the vertices are both white, then turn them black, and vice versa.



Determine if it is possible to turn all the vertices black. If the answer is positive, find the minimum number of times the operation needs to be performed in order to achieve the objective.

Constraints

* 2≤N≤10^5
* N-1≤M≤N
* 1≤a_i,b_i≤N
* There are no self-loops or multiple edges in the given graph.
* The given graph is connected.

Input

The input is given from Standard Input in the following format:


N M
a_1 b_1
a_2 b_2
:
a_M b_M


Output

If it is possible to turn all the vertices black, print the minimum number of times the operation needs to be performed in order to achieve the objective. Otherwise, print `-1` instead.

Examples

Input

6 5
1 2
1 3
1 4
2 5
2 6


Output

5


Input

3 2
1 2
2 3


Output

-1


Input

4 4
1 2
2 3
3 4
4 1


Output

2


Input

6 6
1 2
2 3
3 1
1 4
1 5
1 6


Output

7

### Response

```cpp
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5e5;
int f[N],fst[N],nxt[N],to[N],vis[N],col[N],stack[N],top,root=1,rcnt,ring[N],n,m,mm,tmp[N];
int abs(int x){return x>0?x:-x;}
void dfs(int u,int fa)
{
    f[u]=col[u]?1:-1;
    for(int i=fst[u];i;i=nxt[i])
    {
        int v=to[i];if(v==fa)continue;
        if((u==ring[1]&&v==ring[rcnt])||(u==ring[rcnt]&&v==ring[1]))continue;
        dfs(v,u);f[u]+=f[v];
    }
}
void prework(int u,int c,int fa)
{
    vis[u]=1,col[u]=c;stack[++top]=u;
    for(int i=fst[u];i;i=nxt[i])
    {
        int v=to[i];if(v==fa)continue;
        if(u==ring[rcnt]&&v==ring[1])continue;
        if(vis[v])
        {
            while(stack[top+1]!=v)
                ring[++rcnt]=stack[top--];
            top+=rcnt;
            root=u;
            continue;
        }
        prework(v,c^1,u);
    }
    --top;
}
void ade(int u,int v){to[++mm]=v,nxt[mm]=fst[u],fst[u]=mm;}
int main()
{
    scanf("%d%d",&n,&m);if(n&1){puts("-1");return 0;}
    for(int i=1,u,v;i<=m;i++)scanf("%d%d",&u,&v),ade(u,v),ade(v,u);
    prework(1,0,0);
    dfs(root,0);
    if(m==n)
    {
        if(rcnt&1)
        {
            
            if(f[root]&1){puts("-1");return 0;}
            int t=f[root]>>1;
            for(int i=1;i<=rcnt;i++)f[ring[i]]-=t;
        }
        else
        {
            if(f[root]){puts("-1");return 0;}
            for(int i=2;i<=rcnt;i++)tmp[i]=f[ring[i]];
            tmp[1]=0;
            sort(tmp+1,tmp+rcnt+1);
            int t=tmp[rcnt>>1];
            for(int i=1;i<=rcnt;i++)f[ring[i]]-=t;
        }
    }
    else
        if(f[root]){puts("-1");return 0;}
    long long ans=0;
    for(int i=1;i<=n;i++)ans+=abs(f[i]);
    cout<<ans<<endl;
}
```