#include <bits/stdc++.h>
using namespace std;
/* nichijou */
template<class T,class U>
bool cmax(T & a, const U & b) {return a < b ? a = b, 1 : 0;}
template<class T,class U>
bool cmin(T & a, const U & b) {return b < a ? a = b, 1 : 0;}
/* data type */
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define F first
#define S second
/* STL container */
typedef vector<int> vi;
typedef vector<ll> vll;
#define SZ(a) ((int)(a).size())
#define ALL(a) begin(a), end(a)
#define CLR(a) (a).clear()
#define BK(a) ((a).back())
#define FT(a) ((a).front())
#define DB(a) (a).pop_back()
#define DF(a) (a).pop_front()
#define PB push_back
#define EB emplace_back
/* I gave you my heart and then you turned around. */
void _BG(const char * s) {cerr<<s<<endl;}
template<class T, class ... TT>
void _BG(const char * s,T a, TT...b)
{
for (size_t c = 0; *s && (c || *s != ','); cerr << *s++)
c += count(ALL("([{"),*s) - count(ALL(")]}"),*s);
cerr << " = " << a;
if (*s) {
cerr << ", ";
++s;
}
_BG(s,b...);
}
#ifdef PR3PONY
#define BG(...) do { \
cerr << __PRETTY_FUNCTION__ << ':' << __LINE__ << ": "; \
_BG(#__VA_ARGS__,__VA_ARGS__); \
} while(0)
#else
#define BG(...)
#endif
/* Good Luck && Have Fun ! */
const int N = 2e5 +87;
int n, m;
vector<array<int, 3>> e;
vector<int> g[N];
int d[N], p[N], cc;
int find(int x) {return p[x] == x ? x : p[x] = find(p[x]);}
void meld(int a, int b)
{
a = find(a), b = find(b);
if (a == b)
return;
--cc;
p[b] = a;
}
void check()
{
cerr << "========\n";
cerr << "d:";
for (int i = 1; i <= n; ++i)
cerr << ' ' << d[i];
cerr << endl;
cerr << "edges: \n";
for (int i = 1; i < n; ++i)
for (int x : g[i]) {
int j = e[x][0] ^ e[x][1] ^ i;
if (j > i)
cerr << i << ' ' << j << ' ' << e[x][2] << endl;
}
assert(is_sorted(d + 1, d + n + 1));
deque<int> q = {1};
vector<int> dis(n + 1, N);
dis[1] = 0;
while (q.size()) {
int u = q.front();
q.pop_front();
for (int i : g[u]) {
int v = e[i][0] ^ e[i][1] ^ u;
int w = dis[u] + e[i][2];
if (w < dis[v]) {
dis[v] = w;
if (e[i][2])
q.push_back(v);
else
q.push_front(v);
}
}
}
for (int i = 1; i <= n; ++i)
assert(dis[i] == d[i]);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(e.size());
g[v].push_back(e.size());
e.push_back({u, v, 1});
}
fill_n(d, n + 1, N);
d[1] = 0;
vector<int> q = {1}, nq;
int mx = 1;
cc = 1;
p[1] = 1;
int ptr = 2;
for (int k = 0; q.size(); ++k) {
while (p[ptr])
++ptr;
while (!(ptr == mx + 1 && cc == 1)) {
d[ptr] = k;
mx = max(mx, ptr);
q.push_back(ptr);
p[ptr] = ptr;
++cc;
for (int i : g[ptr]) {
int v = e[i][0] ^ e[i][1] ^ ptr;
if (p[v]) {
e[i][2] = 0;
meld(v, ptr);
}
}
while (p[ptr])
++ptr;
}
nq.clear();
for (int u : q) {
for (int i : g[u]) {
int v = e[i][0] ^ e[i][1] ^ u;
if (d[v] != N)
continue;
d[v] = k + 1;
mx = max(mx, v);
++cc, p[v] = v;
meld(v, u);
nq.push_back(v);
}
}
swap(q, nq);
}
//for (int i = 1; i <= n; ++i) cerr << d[i] << " "; cerr << endl;
ll ans = 0;
for (int i = 1; i <= n; ++i) {
ans += d[i];
}
cout << ans << endl;
check();
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Ci8qIG5pY2hpam91ICovCnRlbXBsYXRlPGNsYXNzIFQsY2xhc3MgVT4KYm9vbCBjbWF4KFQgJiBhLCBjb25zdCBVICYgYikge3JldHVybiBhIDwgYiA/IGEgPSBiLCAxIDogMDt9CnRlbXBsYXRlPGNsYXNzIFQsY2xhc3MgVT4KYm9vbCBjbWluKFQgJiBhLCBjb25zdCBVICYgYikge3Jld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IGkgPDwgJyAnIDw8IGogPDwgJyAnIDw8IGVbeF1bMl0gPDwgZW5kbDsKICAgICAgICB9CiAgICBhc3NlcnQoaXNfc29ydGVkKGQgKyAxLCBkICsgbiArIDEpKTsKICAgIGRlcXVlPGludD4gcSA9IHsxfTsKICAgIHZlY3RvcjxpbnQ+IGRpcyhuICsgMSwgTik7CiAgICBkaXNbMV0gPSAwOwogICAgd2hpbGUgKHEuc2l6ZSgpKSB7CiAgICAgICAgaW50IHUgPSBxLmZyb250KCk7CiAgICAgICAgcS5wb3BfZnJvbnQoKTsKICAgICAgICBmb3IgKGludCBpIDogZ1t1XSkgewogICAgICAgICAgICBpbnQgdiA9IGVbaV1bMF0gXiBlW2ldWzFdIF4gdTsKICAgICAgICAgICAgaW50IHcgPSBkaXNbdV0gKyBlW2ldWzJdOwogICAgICAgICAgICBpZiAodyA8IGRpc1t2XSkgewogICAgICAgICAgICAgICAgZGlzW3ZdID0gdzsKICAgICAgICAgICAgICAgIGlmIChlW2ldWzJdKQogICAgICAgICAgICAgICAgICAgIHEucHVzaF9iYWNrKHYpOwogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgICAgIHEucHVzaF9mcm9udCh2KTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47ICsraSkKICAgICAgICBhc3NlcnQoZGlzW2ldID09IGRbaV0pOwp9CmludCBtYWluKCkKewogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgICBjaW4udGllKDApOwogICAgY2luID4+IG4gPj4gbTsKICAgIGZvciAgKGludCBpID0gMDsgaSA8IG07ICsraSkgewogICAgICAgIGludCB1LCB2OwogICAgICAgIGNpbiA+PiB1ID4+IHY7CiAgICAgICAgZ1t1XS5wdXNoX2JhY2soZS5zaXplKCkpOwogICAgICAgIGdbdl0ucHVzaF9iYWNrKGUuc2l6ZSgpKTsKICAgICAgICBlLnB1c2hfYmFjayh7dSwgdiwgMX0pOwogICAgfQogICAgZmlsbF9uKGQsIG4gKyAxLCBOKTsKICAgIGRbMV0gPSAwOwogICAgdmVjdG9yPGludD4gcSA9IHsxfSwgbnE7CiAgICBpbnQgbXggPSAxOwogICAgY2MgPSAxOwogICAgcFsxXSA9IDE7CiAgICBpbnQgcHRyID0gMjsKICAgIGZvciAoaW50IGsgPSAwOyBxLnNpemUoKTsgKytrKSB7CiAgICAgICAgd2hpbGUgKHBbcHRyXSkKICAgICAgICAgICAgKytwdHI7CiAgICAgICAgd2hpbGUgKCEocHRyID09IG14ICsgMSAmJiBjYyA9PSAxKSkgewogICAgICAgICAgICBkW3B0cl0gPSBrOwogICAgICAgICAgICBteCA9IG1heChteCwgcHRyKTsKICAgICAgICAgICAgcS5wdXNoX2JhY2socHRyKTsKICAgICAgICAgICAgcFtwdHJdID0gcHRyOwogICAgICAgICAgICArK2NjOwogICAgICAgICAgICBmb3IgKGludCBpIDogZ1twdHJdKSB7CiAgICAgICAgICAgICAgICBpbnQgdiA9IGVbaV1bMF0gXiBlW2ldWzFdIF4gcHRyOwogICAgICAgICAgICAgICAgaWYgKHBbdl0pIHsKICAgICAgICAgICAgICAgICAgICBlW2ldWzJdID0gMDsKICAgICAgICAgICAgICAgICAgICBtZWxkKHYsIHB0cik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgd2hpbGUgKHBbcHRyXSkKICAgICAgICAgICAgICAgICsrcHRyOwogICAgICAgIH0KCiAgICAgICAgbnEuY2xlYXIoKTsKICAgICAgICBmb3IgKGludCB1IDogcSkgewogICAgICAgICAgICBmb3IgKGludCBpIDogZ1t1XSkgewogICAgICAgICAgICAgICAgaW50IHYgPSBlW2ldWzBdIF4gZVtpXVsxXSBeIHU7CiAgICAgICAgICAgICAgICBpZiAoZFt2XSAhPSBOKQogICAgICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgZFt2XSA9IGsgKyAxOwogICAgICAgICAgICAgICAgbXggPSBtYXgobXgsIHYpOwogICAgICAgICAgICAgICAgKytjYywgcFt2XSA9IHY7CiAgICAgICAgICAgICAgICBtZWxkKHYsIHUpOwogICAgICAgICAgICAgICAgbnEucHVzaF9iYWNrKHYpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHN3YXAocSwgbnEpOwogICAgfQogICAgLy9mb3IgKGludCBpID0gMTsgaSA8PSBuOyArK2kpIGNlcnIgPDwgZFtpXSA8PCAiICI7IGNlcnIgPDwgZW5kbDsKICAgIGxsIGFucyA9IDA7CiAgICBmb3IgIChpbnQgaSA9IDE7IGkgPD0gbjsgKytpKSB7CiAgICAgICAgYW5zICs9IGRbaV07CiAgICB9CiAgICBjb3V0IDw8IGFucyA8PCBlbmRsOwogICAgY2hlY2soKTsKfQoK