[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i, begin, end) for (int i = begin; i <= end; i ++)
using namespace std;
struct P{
int a, b;
}p[10005];
const int MAXN = 1005;
struct Disjoint_Sets{
struct Sets{
int father, num;
}S[MAXN];
void Init(int n){
for (int i = 0; i <= n; i ++){
S[i].father = i;
S[i].num = 1;
}
}
int Father(int x){
if (S[x].father == x){
return x;
}
else{
S[x].father = Father(S[x].father); //Path compression
return S[x].father;
}
}
void Union(int x, int y){
int fx = Father(x), fy = Father(y);
S[fy].num += S[fx].num;
S[fx].father = fy;
}
}DS;
void uni(int x, int y){
int xx = DS.Father(x);
while(DS.Father(xx) != DS.Father(y)){
DS.Union(xx, xx+1);
xx = DS.Father(xx);
}
}
int main(){
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i ++){
scanf("%d %d", &p[i].a, &p[i].b);
if (p[i].b < p[i].a) swap(p[i].b, p[i].a);
}
int res = 0x3fffffff;
for (int l = 1; l <= n; l ++){
DS.Init(n);
for (int i = 0; i < m; i ++){
if (p[i].a <= l && p[i].b >= (l+1)%n){
uni(0, p[i].a);
uni(p[i].b, n);
}
else uni(p[i].a, p[i].b);
}
int sum = 0;
bool vis[1005] = {0};
for (int i = 1; i <= n; i ++){
if (!vis[DS.Father(i)] && DS.S[DS.Father(i)].num > 1){
sum += DS.S[DS.Father(i)].num - 1;
vis[DS.Father(i)] = 1;
}
}
res = min(res, sum);
}
printf("%d\n", res);
return 0;
}
[/cpp]