#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;
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;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
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 v : g[ptr])
if (p[v])
meld(v, ptr);
while (p[ptr])
++ptr;
}
nq.clear();
for (int u : q) {
for (int v : g[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;
}