#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define even(x) (x % 2 == 0 ? true : false)
#define odd(x) (x % 2 ? true : false)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned;
using pii = pair<int, int>;

//const ll INF = 1e18L + 5;
//const int INF = 1e9 + 5;
//const double pi = 3.14159265358979323846;
//const int mod = 1000000007;
//vector<vector<int>> dp(m, vector<int>(n));

void 	dfs(int p, vector<vector<int>>& paths, vector<bool>& visited)
{
	if (visited[p]) return ;

	visited[p] = true;
	for (int path : paths[p])
		dfs(path, paths, visited);
}

void	solve()
{
	int n, m;
	cin >> n >> m;

	vector<int> a(m), b(m);
	vector<vector<int>> paths(n);
	int ans = 0;

	while (m--) {
		int a, b;
		cin >> a >> b;
		paths[a - 1].push_back(b - 1);
	}

	for (int i = 0; i < n; i++) {
		vector<bool> visited(n);

		dfs(i, paths, visited);
		ans += accumulate(all(visited), 0);
	}
	
	cout << ans << endl;
}

int		main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define M 1000000007
#define FOR(i, s, n) for (ll i = (s); i < (n); ++i)
#define FORALL(i, n) for (ll i = 0; i < (n); ++i)
#define MOD 1000000007
#define ff first
#define ss second
#define pll pair<long long, long long>
#define pii pair<int,int>
#define fast ios_base::sync_with_stdio(false)
#define countbits(x) __builtin_popcount(x)
#define pb(x) push_back((x))
#define vll vector<ll>
#define all(x) x.begin(),x.end()
#define print(x) cout << x << '\n';
#define debug print("reaching\n")
#define mll map<ll,ll>
#define lsb(i) i&(-i)
#define printv(x) \
	FORALL(i, x.size()) \
	{ \
		cout << x[i] << ' '; \
	} \
	cout << endl \
;
#define printm(m) \
	for(auto i: m){\
		cout<<i.ff<<' '<<i.ss<<endl; \
	}\
;

template <typename T>
T extGcd(T a, T b, T &x, T &y)
{
	if (b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	else
	{
		int g = extGcd(b, a % b, y, x);
		y -= a / b * x;
		return g;
	}
}

template <typename T>
T modInv(T a, T m)
{
	T x, y;
	extGcd(a, m, x, y);
	return (x % m + m) % m;
}


long long binpow(long long a, long long b, ll mod) {
    a %= mod;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    res%=mod;
    return res;
}


int main()
{
	// fast;
	// cin.tie(0);
	// cout.tie(0);
	// #ifndef ONLINE_JUDGE
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
	// #endif

	int t=1;
	// cin>>t;
	ll tot=t;
	while(t--){
		ll n,k;
		cin>>n>>k;
		ll x;
		ll ans = 0;
		FOR(y, 2, 2*n+1){
			ll num = k + y;
			if(num < 2) continue;
			if(num > 2*n) continue;
			ll mid = n + 1;
			// has n ways
			ll dist = abs(num  - mid);
			ll dist2 = abs(y - mid);
			ans += (n - dist) * (n - dist2);
		}
		print(ans);
	}

	return 0;
}

/*

*/