#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
typedef std::string str;
#define sei set<int>
#define sell set<ll>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define vvll vector<vll>
#define vld vector<ld>
#define vstr vector<str>
#define vpii vector<pii>
#define vpll vector<pll>
#define all(v) v.begin(),v.end()
#define foa(i,v) for(auto i : v)
#define fo(i,a,b) for(int i=a;i<b;i++)
#define fo_(i,a,b) for(int i=a;i>b;i--)
#define M(a) memset(a,0,sizeof a)
#define M_(a) memset(a ,-1,sizeof a)
#define deb(x)  cerr << #x << " = " << x << endl
#define pb push_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define OK order_of_key
#define FO find_by_order
#define nmax 100100
const ld PI = 3.141592653589793238462643383279;
const ll inf = std::numeric_limits<ll>::max();
const int infint = std::numeric_limits<int>::max();
const ll mod = 1e9+7;
using namespace __gnu_pbds;
using namespace std;
/*#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")*/
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int a[nmax];
int mn[nmax];

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    cin >> n;
    fo(i,0,n) cin >> a[i];
    int mnnow = -1e9;
    fo_(i,n-1,-1){
        mnnow = max(mnnow,a[i]-i);
        mn[i] = mnnow + i+1;
    }
    fo(i,1,n) mn[i] = max(mn[i-1],mn[i]);
    ll ans = 0;
    fo(i,0,n) ans += mn[i]-a[i]-1;
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b) - 1; i >= (a); i--)

int n;
vector<int> v;

int main(void)
{
	ios::sync_with_stdio(false);
	cin >> n;
	v.resize(n);
	vector<int>u(n);
	rep(i, 0, n) cin>>v[i],u[i] = v[i];
	per(i,0,n-1) v[i] = max(v[i],v[i+1]-1);
	rep(i,1,n) v[i]=max(v[i],v[i - 1]);
	ll s=0;
	rep(i,0,n)
		s+=v[i]-u[i];
	cout<<s<<"\n";
}


#include <bits/stdc++.h>

#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int) (x).size())

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<ll> a(n), v(n);
    rep(i, 0, n) cin >> a[i];
    ll mx = -1e9;
    for(int i = n - 1; i >= 0; i--) {
        mx = max(mx - 1, a[i] + 1);
        v[i] = mx;
    }
    ll sum = 0;
    rep(i, 0, n) {
        if(i > 0) v[i] = max(v[i], v[i - 1]);
        sum += v[i] - a[i] - 1;
    }
    cout << sum << '\n';
}


#ifndef LOCAL
#define NDEBUG 1
#endif
#include<bits/stdc++.h>

int main(){
	std::ios::sync_with_stdio(0);std::cin.tie(0);
	int numDay; std::cin>>numDay;
	std::vector<int> numMarkAbove(numDay);
	for(auto & it: numMarkAbove) std::cin>>it;

	auto minNumOtherMarks=numMarkAbove;
	for(auto index=numDay;--index;){
		minNumOtherMarks[index-1]=std::max(minNumOtherMarks[index-1],
				minNumOtherMarks[index]-1);
	}
	for(auto index=1; index<numDay;++index){
		minNumOtherMarks[index]=std::max(minNumOtherMarks[index],
				minNumOtherMarks[index-1]);
	}

	std::cout<<std::accumulate(begin(minNumOtherMarks),end(minNumOtherMarks),
			-std::accumulate(begin(numMarkAbove),end(numMarkAbove),(int64_t)0)
			)<<'\n';
}


#include <iostream>
#include <algorithm>
using namespace std;
#pragma warning (disable: 4996)

int N, A[1 << 18], B[1 << 18]; long long s = 0;

int main() {
	scanf("%d", &N);
	for (int i = 1; i <= N; i++) { scanf("%d", &A[i]); B[i] = A[i] + 1; }
	for (int i = 1; i <= N; i++) B[i] = max(B[i], B[i - 1]);
	for (int i = N - 1; i >= 1; i--) B[i] = max(B[i], B[i + 1] - 1);
	for (int i = 1; i <= N; i++) s += 1LL * (B[i] - A[i] - 1LL);
	cout << s << endl;
	return 0;
}


// In the name of Allah.
// We're nothing and you're everything.
// Ya Ali!

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

const int maxn = 1e5 + 14;
int n, m[maxn], cnt[maxn];
int main(){
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> m[i];
		cnt[i] = max(i ? cnt[i - 1] : 0, m[i] + 1);
	}
	for(int i = n - 1; i >= 0; i--)
		cnt[i] = max(cnt[i], cnt[i + 1] - 1);
	ll ans = 0;
	for(int i = 0; i < n; i++)
		ans += cnt[i] - m[i] - 1;
	cout << ans << '\n';
}


#include <bits/stdc++.h>
#define rep(i,s,t) for(int i=(s),_t=(t);i<_t;++i)
#define per(i,s,t) for(int i=(t)-1,_s=(s);i>=_s;--i)
using namespace std;
typedef long long ll;
typedef double db;
template<class T>void rd(T &x)
{
	x=0;
	static char c;
	while(c=getchar(),c<48);
	do x=x*10+(c^48);
		while(c=getchar(),c>47);
}
template<class T>inline void Max(T &a,T b)
{
	if(b>a)a=b;
}
// EOT

const int N=(int)1e5+5;

int arr[N],mn[N];

int main()
{
	int n;
	ll ans=0;
	cin>>n;
	rep(i,0,n)
	{
		rd(arr[i]);
		mn[i]=arr[i]+1;
		if(i)Max(mn[i],mn[i-1]);
	}
	per(i,1,n)Max(mn[i-1],mn[i]-1);
	rep(i,0,n)ans+=mn[i]-1-arr[i];
	cout<<ans<<endl;
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native")
#include "bits/stdc++.h"
#include <assert.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define PB push_back
#define MP make_pair
const int MOD=1000000007;
#define endl "\n"
#define fst first
#define snd second
const int UNDEF = -1;
const int INF=1<<30;
template<typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; }
template<typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; }
typedef pair<ll,ll> pll;typedef vector<ll> vll;typedef pair<int,int> pii;typedef vector<int> vi;typedef vector<vi> vvi;
#ifdef ONLINE_JUDGE
#define assert(XXX) XXX
#else
#define DEBUG_CAT
#endif
#ifdef DEBUG_CAT
#define dbg(...)   printf( __VA_ARGS__ )
#else
#define dbg(...)   /****nothing****/
#endif
int rint();char rch();long long rlong();
const int mn=(1e5)+4;
int a[mn];
int vneed[mn];
int main()
{
  ios_base::sync_with_stdio(false); cin.tie(0);
  int n=rint();
  for (int i=0;i<n;i++) a[i]=rint();
  for (int i=n-2;i>=0;i--) {
    vneed[i]=max(vneed[i+1]-1,a[i+1]);
  }
  for (int i=1;i<n;i++) {
    chkmax(vneed[i],max(a[i]+1,vneed[i-1]));
  }
  ll ans=0;
  for (int i=0;i<n;i++) {
    //printf("i:%d vneed:%d\n",i,vneed[i]);
    int contrib=max(0,vneed[i]-a[i]-1);
    ans+=contrib;
  }
  printf("%lld\n",ans);
}

static char stdinBuffer[1024];
static char* stdinDataEnd = stdinBuffer + sizeof (stdinBuffer);
static const char* stdinPos = stdinDataEnd;

void readAhead(size_t amount)
{
    size_t remaining = stdinDataEnd - stdinPos;
    if (remaining < amount) {
       memmove(stdinBuffer, stdinPos, remaining);
       size_t sz = fread(stdinBuffer + remaining, 1, sizeof (stdinBuffer) - remaining, stdin);
       stdinPos = stdinBuffer;
       stdinDataEnd = stdinBuffer + remaining + sz;
       if (stdinDataEnd != stdinBuffer + sizeof (stdinBuffer))
         *stdinDataEnd = 0;
    }
}

int rint()
{
    readAhead(16);

    int x = 0;
    bool neg = false;
    while(*stdinPos==' '||*stdinPos=='\n') ++stdinPos;
    if (*stdinPos == '-') {
       ++stdinPos;
       neg = true;
    }

    while (*stdinPos >= '0' && *stdinPos <= '9') {
       x *= 10;
       x += *stdinPos - '0';
       ++stdinPos;
    }

    return neg ? -x : x;
}
char rch()
{
    readAhead(16);
    while(*stdinPos==' '||*stdinPos=='\n') ++stdinPos;
    char ans=*stdinPos;
    ++stdinPos;
    return ans;
}
long long rlong()
{
    readAhead(32);

    long long x = 0;
    bool neg = false;
    while(*stdinPos==' '||*stdinPos=='\n') ++stdinPos;
    if (*stdinPos == '-') {
       ++stdinPos;
       neg = true;
    }

    while (*stdinPos >= '0' && *stdinPos <= '9') {
       x *= 10;
       x += *stdinPos - '0';
       ++stdinPos;
    }

    return neg ? -x : x;
}


#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<pair<int, int> > vpii;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
#define INF 0x3f3f3f3f
#define MOD 998244353LL
#define EPSILON 0.00001
#define f first
#define s second
#define pb push_back
#define mp make_pair

#define FOR(i, a, b) for (int i=(a); i<=(signed)(b); i++)
#define F0R(i, a) for (int i=0; i<(signed)(a); i++)
#define RFOR(i, a, b) for (int i=(a); i >= b; i--)

#define MN 100005
int n;
ll m[MN];
ll tot[MN];
ll mx[MN];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    FOR(i, 1, n){
        cin >> m[i];
        mx[i] = max(mx[i-1], m[i]+1);
    }
    tot[n] = mx[n];
    ll sm = 0;
    RFOR(i, n-1, 1){
        tot[i] = max(tot[i+1]-1, mx[i]);
    }
    FOR(i, 1, n){
        sm = sm+tot[i]-m[i]-1;
    }

    cout << sm << "\n";

    return 0;
}


#include <bits/stdc++.h>

//#include <unistd.h>
//#include <iostream>

using namespace std;

#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl;
#define DEBUG_MAT(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;}
typedef long long ll;
//#define int ll

#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); }
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); }
template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
#define X first
#define Y second
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(),c.end()
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; }
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const long double pi = 3.1415926535897932384626433832795028841971L;
#define Sp(p) cout<<setprecision(25)<< fixed<<p<<endl;
//int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
//int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
vi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
vi dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 };
#define fio() cin.tie(0); ios::sync_with_stdio(false);
const ll MOD = 1000000007;
//const ll MOD = 998244353;
// #define mp make_pair
//#define endl '\n'

signed main() {
  fio();
  int n;
  cin >> n;
  vi m(n);
  rep (i, n) {
    cin >> m[i];
  }

  vector<pii> xi(n);
  rep (i, n) {
    xi[i].second = i;
    xi[i].first = i - m[i];
  }
  sort(all(xi));

  int xj = 0;
  int num = 0;
  int sub = 0;
  ll ans = 0;

  rep (i, n) {
    ans += num - m[i];
    while (xj < n and xi[xj].second <= i) xj++;
    if (xi[xj].first - sub == 0) num++;
    else sub++;
  }
  cout << ans << endl;
}

// signed main() {
//   fio();
//   ll n, u;
//   cin >> n >> u;
//   vl e(n);
//   rep (i, n) cin >> e[i];

//   bool flag = false;
//   rep (i, n - 2) {
//     if (e[i + 2] - e[i] <= u) flag = true;
//   }
//   if (not flag) {
//     cout << -1 << endl;
//     return 0;
//   }

//   double ok = 0, ng = 1;
//   rep (_, 100) {
//     double x = (ok + ng) / 2;
//     using P = pair<double, int>;
//     deque<P> deq;

//     bool flag = false;
//     rep (i, n) {
//       while (deq.size() > 0) {
//         int j = deq[0].second;
//         if (e[i] - e[j] > u) {
//           deq.pop_front();
//         }
//         else break;
//       }

//       if (deq.size() > 0) {
//         double ma = deq[0].first;
//         if (e[i] >= ma) {
//           flag = true;
//           break;
//         }
//       }

//       if (i >= 1) {
//         double add = (e[i] - x * e[i - 1]) / (1 - x);
//         while (deq.size() > 0) {
//           if (deq.back().first >= add) deq.pop_back();
//           else break;
//         }
//         deq.push_back(P(add, i - 1));
//       }
//     }

//     if (flag) ok = x;
//     else ng = x;
//   }

//   Sp(ok);
// }


#include<bits/stdc++.h>
#define oo 999999999
using namespace std;
typedef long long int lli;
lli gcd(lli a,lli b) {if(a==0||b==0) return 0;if(a==b) return a;if(a>b) return gcd(a-b,b);return gcd(a,b-a);}
lli max(lli a,lli b) {if(a>b) return a; return b;}
lli min(lli a,lli b) {if(a>b) return b; return a;}
int main()
{
	ios_base::sync_with_stdio;
	cin.tie(NULL);
	lli n;
	cin>>n;
	lli arr[n],t[n];
	for(lli i=0;i<n;i++)
	{
		cin>>arr[i];
		t[i]=i+1;
	}
	for(lli i=1;i<n;i++)
		t[i]=max(t[i-1],arr[i]+1);
	for(lli i=n-1;i>0;i--)
		t[i-1]=max(t[i]-1,t[i-1]);
	lli answer=0;
	for(lli i=0;i<n;i++)
		answer+=t[i]-1-arr[i];
	cout<<answer;
	int dont_hack_please;
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn  = 1e5+10;
int dat[maxn];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int maxh=0,maxi=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&dat[i]);
            if(dat[i]>maxh)
            {
                maxh=dat[i];
                maxi=i;
            }
        }
        ll ans = 0;
        for(int i=maxi+1;i<=n;i++)
        {
            if(dat[i]<maxi)ans+=maxh-dat[i];
        }
     //   cout<<" ans = "<<ans<<endl;
        int k=maxh-1;
        for(int i=maxi-1;i>=1;i--)
        {
            k=max(k,dat[i]);
         //   cout<<"i = "<<i<<" k = "<<k<<endl;
            if(dat[i]<k)
            {
                ans+=1ll*(k-dat[i]);
            }
            k--;
        }
        printf("%lld\n",ans);
    }
}


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn  = 1e5+10;
int dat[maxn];
int mins[maxn];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int maxh=0,maxi=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&dat[i]);
            if(dat[i]>maxh)
            {
                maxh=dat[i];
                maxi=i;
            }
            mins[i]=maxh;
        }
        ll ans = 0;
        for(int i=maxi+1;i<=n;i++)
        {
            if(dat[i]<maxi)ans+=maxh-dat[i];
        }
     //   cout<<" ans = "<<ans<<endl;
        int k=maxh-1;
        for(int i=maxi-1;i>=1;i--)
        {
            k=max(k,mins[i]);
         //   cout<<"i = "<<i<<" k = "<<k<<endl;
            if(dat[i]<k)
            {
                ans+=1ll*(k-dat[i]);
            }
            k--;
        }
        printf("%I64d\n",ans);
    }
}


#include <bits/stdc++.h>
#include <fstream>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define bs binary_search
#define vl vector<ll>
#define pll pair<ll,ll>
#define vpl vector<pair<ll,ll> >
#define mll map<ll,ll>
#define mcl map<char,ll>
#define msl map<string,ll>
#define mset memset
#define fr(a,b,c) for(ll a=b;a<c;a++)
void in(ll *a,ll n){fr(i,0,n){cin>>a[i];}}
void out(ll *a,ll n){fr(i,0,n){cout<<a[i]<<" ";}}
void fast(){
ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int main(){
	fast();
	ll n;
	cin>>n;
	ll a[n];
	ll ans=0;
	ll max=0;
	fr(i,0,n){
		cin>>a[i];
		if(a[i]>max){
			max=a[i];
		}
	}
	max++;
	for(ll i=n-1;i>=0;i--){
		max=min(i+1,max);
		ans=ans+(max-a[i]-1);
	}
	cout<<ans;
}


#include <bits/stdc++.h>
#include <fstream>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define bs binary_search
#define vl vector<ll>
#define pll pair<ll,ll>
#define vpl vector<pair<ll,ll> >
#define mll map<ll,ll>
#define mcl map<char,ll>
#define msl map<string,ll>
#define mset memset
#define fr(a,b,c) for(ll a=b;a<c;a++)
void in(ll *a,ll n){fr(i,0,n){cin>>a[i];}}
void out(ll *a,ll n){fr(i,0,n){cout<<a[i]<<" ";}}
void fast(){
ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int main(){
	fast();
	ll n;
	cin>>n;
	ll a[n];
	ll ans=0;
	ll maxi=0;
	fr(i,0,n){
		cin>>a[i];
		if(a[i]>maxi){
			maxi=a[i];
		}
	}
	maxi++;
	ll b[n];
	b[0]=a[0]+1;
	fr(i,1,n){
		b[i]=max(a[i]+1,b[i-1]);
	}
	for(ll i=n-1;i>=1;i--){
		if((b[i]-1)==b[i-1]||b[i]==b[i-1]){

		}
		else{
			b[i-1]=b[i]-1;
		}
	}

	for(ll i=n-1;i>0;i--){
		ans=ans+(b[i]-a[i]-1);
	}
	cout<<ans;
}


#include <bits/stdc++.h>
#include <fstream>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define bs binary_search
#define vl vector<ll>
#define pll pair<ll,ll>
#define vpl vector<pair<ll,ll> >
#define mll map<ll,ll>
#define mcl map<char,ll>
#define msl map<string,ll>
#define mset memset
#define fr(a,b,c) for(ll a=b;a<c;a++)
void in(ll *a,ll n){fr(i,0,n){cin>>a[i];}}
void out(ll *a,ll n){fr(i,0,n){cout<<a[i]<<" ";}}
void fast(){
ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int main(){
	fast();
	ll n;
	cin>>n;
	ll a[n];
	ll ans=0;
	ll max=0;
	fr(i,0,n){
		cin>>a[i];
		if(a[i]>max){
			max=a[i];
		}
	}
	max++;
	for(ll i=n-1;i>0;i--){
		max=min(i+1,max);

		ans=ans+(max-a[i]-1);
		if(a[i-1]+1==a[i]){
			max--;
		}
	}
	cout<<ans;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define FI first
#define SE second
#define pb push_back
#define eb emplace_back
#define mod 1000000007
#define all(c) (c).begin(),(c).end()
#define LB  lower_bound
#define UB  upper_bound
#define max3(a,b,c) max(c,max(a,b))
#define min3(a,b,c) min(c,min(a,b))
#define mems(s, n) memset(s, n, sizeof(s))
#define NINF -1e18
#define INF 1e18
#define int ll int
#define OOK order_of_key  //no of elements less than
#define FBO find_by_order //iterator pointing kth element;indexing starts from 0
#define CK3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define CK4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl

typedef pair<int,int> PII;
typedef pair<pair<int,int>,int> PPII;
typedef pair<int,pair<int,int>> PIPI;
typedef map<int,int>MII;
typedef vector<int> VI;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

/*int power(int a, int b)
{int x=1,y=a;while(b>0){if(b%2){x =(x*y)%mod;}y =(y*y)%mod;b/=2;}return x%mod;}
*/
//int modInverse(int a, int m) {return power(a, m-2);} //gcd(a,m)=1

signed main()
{
ios::sync_with_stdio(false); cin.tie(0);
int T=1,T1=0;//cin>>T;
while(T1++<T)
{//cout<<"Case #"<<T1<<": ";
    int n;
    cin>>n;VI ar(n);int sum=0;
    for(int i=0;i<n;i++)cin>>ar[i];
    int total=1;set<int>se;
    for(int i=1;i<n;i++)
    {
        if(total!=ar[i])
        {   if(ar[i]<total)
            {sum+=(total-ar[i]-1);se.insert(i);}
            else
            {

            	while(ar[i]>total)
            	{    auto it=se.end();it--;
            	     sum+=(i-*it);
            		total++;se.erase(it);
            	}
            	total++;
            }
        }
        else
        total++;
    }
    cout<<sum<<endl;
}

return 0;
}


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

using ll=long long;
#define int ll

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.a<<","<<p.b<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ";
	dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif

using uint=unsigned;
using ull=unsigned long long;

template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
	return os<<vc<t>(all(a));
}

template<int i,class T>
void print_tuple(ostream&,const T&){
}

template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
	if(i)os<<",";
	os<<get<i>(t);
	print_tuple<i+1,T,Args...>(os,t);
}

template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
	os<<"{";
	print_tuple<0,tuple<Args...>,Args...>(os,t);
	return os<<"}";
}

template<class t>
void print(t x,int suc=1){
	cout<<x;
	if(suc==1)
		cout<<"\n";
	if(suc==2)
		cout<<" ";
}

ll read(){
	ll i;
	cin>>i;
	return i;
}

vi readvi(int n,int off=0){
	vi v(n);
	rep(i,n)v[i]=read()+off;
	return v;
}

template<class T>
void print(const vector<T>&v,int suc=1){
	rep(i,v.size())
		print(v[i],i==int(v.size())-1?suc:2);
}

string readString(){
	string s;
	cin>>s;
	return s;
}

template<class T>
T sq(const T& t){
	return t*t;
}

//#define CAPITAL
void yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<"\n";
	#else
	cout<<"Yes"<<"\n";
	#endif
	if(ex)exit(0);
}
void no(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<"\n";
	#else
	cout<<"No"<<"\n";
	#endif
	if(ex)exit(0);
}
void possible(bool ex=true){
	#ifdef CAPITAL
	cout<<"POSSIBLE"<<"\n";
	#else
	cout<<"Possible"<<"\n";
	#endif
	if(ex)exit(0);
}
void impossible(bool ex=true){
	#ifdef CAPITAL
	cout<<"IMPOSSIBLE"<<"\n";
	#else
	cout<<"Impossible"<<"\n";
	#endif
	if(ex)exit(0);
}

constexpr ll ten(int n){
	return n==0?1:ten(n-1)*10;
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

int topbit(signed t){
	return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
	return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
	return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
	return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
	return __builtin_popcount(t);
}
int popcount(ll t){
	return __builtin_popcountll(t);
}
bool ispow2(int i){
	return i&&(i&-i)==i;
}
int mask(int i){
	return (int(1)<<i)-1;
}

bool inc(int a,int b,int c){
	return a<=b&&b<=c;
}

template<class t> void mkuni(vc<t>&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

ll rand_int(ll l, ll r) { //[l, r]
	#ifdef LOCAL
	static mt19937_64 gen;
	#else
    static random_device rd;
    static mt19937_64 gen(rd());
    #endif
    return uniform_int_distribution<ll>(l, r)(gen);
}

template<class t>
int lwb(const vc<t>&v,const t&a){
	return lower_bound(all(v),a)-v.bg;
}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);

	int n;cin>>n;
	vi m=readvi(n);
	vi a=m;rep(i,n)a[i]++;
	per(i,n-1)chmax(a[i],a[i+1]-1);
	dmp(a);
	rep(i,n-1)chmax(a[i+1],a[i]);
	dmp(a);
	int ans=0;
	rep(i,n)ans+=a[i]-m[i];
	print(ans-n);
}


//By UditDesai
#pragma GCC optimize ("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define memreset(a) memset(a,0,sizeof(a))
#define testcase(t) int t;cin>>t;while(t--)
#define trav(i,v) for(auto &i: v)
#define forn(i,e) for(int i=0;i<e;++i)
#define forsn(i,s,e) for(int i=s;i<e;++i)
#define rforn(i,s) for(int i=s;i>=0;--i)
#define rforsn(i,s,e) for(int i=s;i>=e;--i)
#define bitcount(a) __builtin_popcount(a) // set bits (add ll)
#define nl cout<<'\n'
#define sp <<' '<<
#define sz(a) (int)(a.size())
#define check(x) cout << #x << " : " << (x) << endl
#define show(a) for(auto i:a) cout<<i<<" ";cout<<endl//By UditDesai
#define getcurrtime() cerr<<"Time = "<<((double)clock()/CLOCKS_PER_SEC)<<endl
#define dbgarr(v,s,e) cerr<<#v<<" = "; forsn(i,s,e) cerr<<v[i]<<", "; cerr<<endl
#define inputfile freopen("input.txt", "r", stdin)
#define outputfile freopen("output.txt", "w", stdout)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> p64;
typedef pair<int,int> p32;
typedef pair<int,p32> p96;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<v32> vv32;
typedef vector<v64> vv64;
typedef vector<p32> vp32;
typedef vector<p64> vp64;
typedef vector<vp32> vvp32;
typedef map<int,int> m32;
const int N=2e5+5,MOD=1e9+7;
const ld EPS = 1e-9;

int main()
{
	fastio;
	ll i,n; cin>>n;
	ll m[n]; forn(i,n) cin>>m[i];
	ll mx[n]={};
	mx[0] = 1;
	for(i=1;i<n;i++) mx[i] = max(mx[i-1],m[i]+1);
	for(i=n-2;i>=0;i--)
	{
		mx[i] = max(mx[i],mx[i+1]-1);
	}
	ll ans = 0;
	//forn(i,n) cout<<mx[i]<<' ';nl;
	for(i=0;i<n;i++) ans += mx[i]-m[i]-1;
	cout<<ans;
	return 0;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <numeric>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <string>
#include <cstdio>
#include <vector>
#include <deque>
#include <cmath>
#include <list>
#include <set>
#include <map>
#define rep(i,m,n) for(int i=(m),_end=(n);i < _end;++i)
#define repe(i,m,n) for(int i=(m), _end =(n);i <= _end;++i)
typedef long long ll;
using namespace std;
#ifdef ONLINE_JUDGE
const bool testing = false;
#else
const bool testing = true;
#endif

int ma[100005], mm[100005];

void program() {

	int n;
	long long res = 0;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> ma[i];
	}
	mm[n] = 0;
	for (int i = n - 1; i >= 0; i--)
	{
		mm[i] = std::max(ma[i] + 1, mm[i + 1] - 1);
	}
	int mks = 0;
	for (int i = 0; i < n; i++)
	{
		mks = std::max(mks, mm[i]);
		res += mks - ma[i] - 1;
	}

	cout << res << endl;
}

int main() {
	if (!testing) { // set testing to false when submiting to codeforces
		program(); // write your program in 'program' function (its your new main !)
		return 0;
	}

	FILE* fin = NULL;

	// Test Case 0:
	fin = fopen("in.txt", "w+");
	fprintf(fin, "\
6\n\
0 1 0 3 0 2\n\
");
	fclose(fin);
	freopen("in.txt", "r", stdin);
	printf("test case(1) => expected : \n");
	printf("\
6\n\
");
	printf("test case(1) => found    : \n");
	program();

	// Test Case 1:
	fin = fopen("in.txt", "w+");
	fprintf(fin, "\
5\n\
0 1 2 1 2\n\
");
	fclose(fin);
	freopen("in.txt", "r", stdin);
	printf("test case(2) => expected : \n");
	printf("\
1\n\
");
	printf("test case(2) => found    : \n");
	program();

	// Test Case 2:
	fin = fopen("in.txt", "w+");
	fprintf(fin, "\
5\n\
0 1 1 2 2\n\
");
	fclose(fin);
	freopen("in.txt", "r", stdin);
	printf("test case(3) => expected : \n");
	printf("\
0\n\
");
	printf("test case(3) => found    : \n");
	program();

	return 0;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <numeric>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <string>
#include <cstdio>
#include <vector>
#include <deque>
#include <cmath>
#include <list>
#include <set>
#include <map>
#define rep(i,m,n) for(int i=(m),_end=(n);i < _end;++i)
#define repe(i,m,n) for(int i=(m), _end =(n);i <= _end;++i)
typedef long long ll;
using namespace std;
#ifdef ONLINE_JUDGE
const bool testing = false;
#else
const bool testing = true;
#endif

int ma[100005], mm[100005];

void program() {

	int n;
	int res = 0;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> ma[i];
	}
	mm[n] = 0;
	for (int i = n - 1; i >= 0; i--)
	{
		mm[i] = std::max(ma[i] + 1, mm[i + 1] - 1);
	}
	int mks = 0;
	for (int i = 0; i < n; i++)
	{
		mks = std::max(mks, mm[i]);
		res += mks - ma[i] - 1;
	}

	cout << res << endl;
}

int main() {
	if (!testing) { // set testing to false when submiting to codeforces
		program(); // write your program in 'program' function (its your new main !)
		return 0;
	}

	FILE* fin = NULL;

	// Test Case 0:
	fin = fopen("in.txt", "w+");
	fprintf(fin, "\
6\n\
0 1 0 3 0 2\n\
");
	fclose(fin);
	freopen("in.txt", "r", stdin);
	printf("test case(1) => expected : \n");
	printf("\
6\n\
");
	printf("test case(1) => found    : \n");
	program();

	// Test Case 1:
	fin = fopen("in.txt", "w+");
	fprintf(fin, "\
5\n\
0 1 2 1 2\n\
");
	fclose(fin);
	freopen("in.txt", "r", stdin);
	printf("test case(2) => expected : \n");
	printf("\
1\n\
");
	printf("test case(2) => found    : \n");
	program();

	// Test Case 2:
	fin = fopen("in.txt", "w+");
	fprintf(fin, "\
5\n\
0 1 1 2 2\n\
");
	fclose(fin);
	freopen("in.txt", "r", stdin);
	printf("test case(3) => expected : \n");
	printf("\
0\n\
");
	printf("test case(3) => found    : \n");
	program();

	return 0;
}


#include <bits/stdc++.h>

#define int         long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)
using namespace std;
int n,a[100005],cur,ans,h[100005],mx;
void solve(){
    cin>>n;
    rep(i,0,n) cin>>a[i];
    for(int i=n-1;i>=0;i--){
        mx=max(mx-1,a[i]);
        h[i]=mx;
    }
    rep(i,1,n) h[i]=max(h[i-1],h[i]);
    rep(i,0,n) ans+=h[i]-a[i];
    cout<<ans<<endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

#define ll          long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)
using namespace std;
int n,a[100005],cur,ans,h[100005],mx=-1,pos;
void solve(){
    cin>>n;
    rep(i,0,n){
        cin>>a[i];
        if(a[i]>mx){
            mx=a[i];
            pos=i;
        }
    }
    rep(i,pos+1,n) h[i]=mx;
    for(int i=pos;i>=0;i--){
        mx=max(mx-1,a[i]);
        h[i]=mx;
    }
    rep(i,1,n) h[i]=max(h[i-1],h[i]);
    rep(i,0,n) ans+=h[i]-a[i];
    cout<<ans<<endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


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

int main()
{
  int n;
  long long sol = 0;
  cin >> n;
  vector<int> a(n), h(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  h[0] = a[0];
  for (int i = 1; i < n; i++)
    h[i] = max(h[i - 1], a[i]);
  for (int i = n - 2; i >= 0; i--)
    h[i] = max(h[i], h[i + 1] - 1);
  for (int i = 0; i < n; i++)
    sol += h[i] - a[i];
  cout << sol << endl;
  return 0;
}


// {{{ by shik
#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REP1(i, a, b) for (int i = (a); i <= int(b); i++)
#define MP make_pair
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef pair<int, int> PII;
typedef vector<int> VI;

namespace { namespace shik {

template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%" SCNd64, &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }

template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%" PRId64, x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }

#ifdef SHIK
#include "dump.hpp"
#else
#define dump(...)
#endif

template<class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) { sort(begin(v), end(v), f); v.resize(unique(begin(v), end(v)) - begin(v)); }
template<class T> inline T bit(T x, int i) { return (x >> i) & 1; }
template<class T> inline bool chkmax(T &a, const T &b) { return b > a ? a = b, true : false; }
template<class T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;

// }}}

const int N=1e5+10;
int n,a[N],dp[N];
void main() {
    R(n);
    REP1(i,1,n) R(a[i]);
    REP1(i,1,n) dp[i]=a[i]+1;
    REP1(i,2,n) chkmax(dp[i],dp[i-1]);
    for ( int i=n-1; i>=1; i-- ) chkmax(dp[i],dp[i+1]-1);
    LL ans=0;
    REP1(i,1,n) ans+=dp[i]-a[i]-1;
    W(ans);
}

// {{{ main
}}
int main() { shik::main(); return 0; }
// }}}


/* be name khoda */

// #define stream_enable
#define long_enable
#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#include <iomanip>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;

#ifdef stream_enable
#define cin sss
#endif
#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<pii> vpii;

const ll MOD = 1000000007;
const long long BIG = 1446803456761533460;
const int Big = 336860180;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()

#define debug(x) cout << #x << " -> " << (x) << endl;
#define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl;
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl;
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl;
#define debugp(x) cout << #x << " -> " << "(" << (x).F << ", " << (x).S << ")" << endl;
#define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl
#define debugap(x, n) cout << #x << " ->\n"; fori (i1_dap, n) { cout << "(" << (x)[i1_dap].F << ", " << (x)[i1_dap].S << ")\n"; } cout << endl
#define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl
#define debugav(x, n) cout << #x << " ->\n"; fori (i1_dav, n) { fori (i2_dav, (x)[i1_dav].size()) { cout << (x)[i1_dav][i2_dav] << ' '; } cout << '\n'; } cout << endl
#define debugia(x, n) cout << #x << " ->\n"; fori (i1_dia, n) { cout << i1_dia << " : " << (x)[i1_dia] << '\n'; } cout << endl

#define forifrom(i, s, n) for(ll i = (s); i < (n); ++i)
#define forirto(i, n, e) for(ll i = (n) - 1; i >= (e); --i)
#define fori(i, n) forifrom (i, 0, n)
#define forir(i, n) forirto (i, n, 0)

#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define Add(a, b) a = (a + (b)) % MOD
#define Mul(a, b) a = (a * (b)) % MOD

stringstream sss;

// -----------------------------------------------------------------------

const ll maxn = 100010;

ll n;
ll ar[maxn];
ll c[maxn];

void MAIN() {

    cin >> n;
    fori (i, n) cin >> ar[i];
    ll s = 0;
    ll mx = 0;
    forir (i, n) {
        c[i] = max(c[i + 1] - 1, ar[i] + 1);
    }
    fori (i, n) {
        smax(c[i + 1], c[i]);
    }
    fori (i, n) {
        s += c[i] - ar[i] - 1;
    }
    cout << s << '\n';

}

// -----------------------------------------------------------------------

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(10);
    sss << R"(
5
0 1 2 1 2
    )";
    MAIN();
    return 0;
}


#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cstdarg>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;

#if __cplusplus > 199711L
    void read(){}
    template<typename... T>
    void read(int& head, T&... tail) {scanf("%d",&head); read(tail...);}

    #define DB(args...) { cerr << __LINE__<< ": "; vector<string> _v = split(#args, ','); err(_v.begin(), args); }
    vector<string> split(const string& s, char c) {
        vector<string> v;stringstream ss(s);string x;
        while (getline(ss, x, c)) v.push_back(x);
        return move(v);
    }
    void err(vector<string>::iterator it) {cerr<<endl;}
    template<typename T, typename... Args>
    void err(vector<string>::iterator it, T a, Args... args) {
        cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << "  "; err(++it, args...);
    }
#else
    #define DB(e) cerr << __LINE__ << ": " << #e << " = " << e << endl;
    void read(int& head) {scanf("%d",&head);}
#endif

#define ll long long int
#define pb push_back
#define fr(i,n)     for(int i=0;i<n;i++)
#define init(mem,v) memset(mem,v,sizeof(mem))
typedef pair<int,int> pii;

#define mx 100005
int m[mx], mm[mx];

int main(){
    int n;
    read(n);
    fr(i,n) read(m[i]);

    int mr=0;
    for(int i=n-1;i>=0;i--){
        mm[i]=mr;
        mr=max(mr-1,m[i]);
    }

    ll ans=0;
    int tot=0;

    fr(i,n){
        if(tot<mm[i]){
            tot+=1;
            ans+=(tot-1-m[i]);
        }
        else{
            ans+=(tot-m[i]);
        }
    }
    cout<<ans<<endl;
}


#include <stdio.h>

int m[100005];
int free[100005];

int min(int a, int b){
    return a < b ? a : b;
}

int main(){
    int n;
    scanf("%d", &n);
    for(int i=1; i<=n; i++) scanf("%d", &m[i]);
    free[n] = n-m[n]-1;
    for(int i=n-1; i>=1; i--){
        free[i] = min(free[i+1], i-m[i]-1);
    }
    int cur_dup = 0;
    long long res = 0;
    for(int i=1; i<=n; i++){
        if(cur_dup < free[i]){
            cur_dup++;
        }
        res += i-m[i]-cur_dup-1;
    }
    printf("%lld", res);
    return 0;
}


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

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int n;
  cin >> n;
  vector<int> m(n), x(n);
  int ma = 0;
  for (int i = 0; i < n; i++) {
    cin >> m[i];
    ma = max(ma, m[i]);
    x[i] = ma+1;
  }
  ll ans = 0;
  int mi = x[n-1]+1;
  for (int i = n-1; i >= 0; i--) {
    mi = max(mi-1, x[i]);
    ans += mi-m[i]-1;
  }
  cout << ans << endl;
}


#include <iostream>

using namespace std;

const int MAX_N = 1e5 + 5;

typedef long long ll;

int arr [MAX_N];
int ans [MAX_N];

int main () {
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }

  int cur = 0;
  for (int i = n - 1; i >= 0; i--) {
    cur = max(cur, arr[i] + 1);
    ans[i] = cur;
    cur = max(cur - 1, 0);
  }

  for (int i = 1; i < n; i++) {
    ans[i] = max(ans[i], ans[i - 1]);
  }

  ll sol = 0;
  for (int i = 0; i < n; i++) {
    sol += ans[i] - arr[i] - 1;
  }

  cout << sol << endl;
}


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

using ll = long long;
using ld = long double;
#define MAX(type) numeric_limits<type>::max()
#define MIN(type) numeric_limits<type>::min()

/*
typedef struct dsu {
    vector<int> link;
    vector<int> size;
} dsu;
dsu init(int n);
int find(dsu& u, int x);
bool same(dsu& u, int a, int b);
void unite(dsu& u, int a, int b);
*/

/*
int modExp(int x, int n, int m);
int mmi(int x, int m);
*/

ll n;
ll m;
vector<vector<int>> adj;
vector<vector<bool>> matrix;

void solve() {
    cin >> n >> m;
    adj.resize(n + 1);
    matrix.resize(n + 1, vector<bool>(n + 1, false));
    for (int i = 0, a, b; i < m; i++) {
        cin >> a >> b;
        adj[a].push_back(b);
        matrix[a][b] = true;
    }

    ll p = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == j) {
                continue;
            }

            ll sum = 0;
            for (auto v1 : adj[i]) {
                if (matrix[v1][j]) {
                    sum++;
                }
            }
            p += sum * (sum - 1) / 2;
        }
    }

    cout << p << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    for (int i = 1; i <= t; i++) {
        solve();
    }
    return EXIT_SUCCESS;
}

/*
int modExp(const int x, const int n, const int m) {
    if (n == 0) {
        return 1 % m;
    }
    LLint u = modExp(x, n / 2, m);
    u = (u * u) % m;
    if (n % 2 == 1) {
        u = (u * x) % m;
    }
    return u;
}
int mmi(const int x, const int p) { return modExp(x, p - 2, p); }
*/

/*
dsu init(int n) {
    dsu u;
    u.link = vector<int>(n + 1);
    u.size = vector<int>(n + 1);
    for (int i = 1; i <= n; i++) {
        u.link[i] = i;
        u.size[i] = 1;
    }
    return u;
}

int find(dsu& u, int x) {
    while (x != u.link[x]) {
        x = u.link[x];
    }
    return x;
}

bool same(dsu& u, int a, int b) { return find(u, a) == find(u, b); }

void unite(dsu& u, int a, int b) {
    a = find(u, a);
    b = find(u, b);
    if (u.size[a] < u.size[b]) {
        swap(a, b);
    }
    u.size[a] += u.size[b];
    u.link[b] = a;
}
*/


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

using ll = long long;
using ld = long double;
#define MAX(type) numeric_limits<type>::max()
#define MIN(type) numeric_limits<type>::min()

/*
typedef struct dsu {
    vector<int> link;
    vector<int> size;
} dsu;
dsu init(int n);
int find(dsu& u, int x);
bool same(dsu& u, int a, int b);
void unite(dsu& u, int a, int b);
*/

/*
int modExp(int x, int n, int m);
int mmi(int x, int m);
*/

ll n;
ll m;
vector<vector<int>> adj;
vector<vector<bool>> matrix;

void solve() {
    cin >> n >> m;
    adj.resize(n + 1);
    matrix.resize(n + 1, vector<bool>(n + 1, false));
    for (int i = 0, a, b; i < m; i++) {
        cin >> a >> b;
        adj[a].push_back(b);
        matrix[a][b] = true;
    }

    ll p = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == j) {
                continue;

            }

            ll sum = 0;
            for (auto v1 : adj[i]) {
                if (matrix[v1][j]) {
                    sum++;
                }
            }
            p += sum * (sum - 1) / 2;
        }
    }

    cout << p << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    for (int i = 1; i <= t; i++) {
        solve();
    }
    return EXIT_SUCCESS;
}

/*
int modExp(const int x, const int n, const int m) {
    if (n == 0) {
        return 1 % m;
    }
    LLint u = modExp(x, n / 2, m);
    u = (u * u) % m;
    if (n % 2 == 1) {
        u = (u * x) % m;
    }
    return u;
}
int mmi(const int x, const int p) { return modExp(x, p - 2, p); }
*/

/*
dsu init(int n) {
    dsu u;
    u.link = vector<int>(n + 1);
    u.size = vector<int>(n + 1);
    for (int i = 1; i <= n; i++) {
        u.link[i] = i;
        u.size[i] = 1;
    }
    return u;
}

int find(dsu& u, int x) {
    while (x != u.link[x]) {
        x = u.link[x];
    }
    return x;
}

bool same(dsu& u, int a, int b) { return find(u, a) == find(u, b); }

void unite(dsu& u, int a, int b) {
    a = find(u, a);
    b = find(u, b);
    if (u.size[a] < u.size[b]) {
        swap(a, b);
    }
    u.size[a] += u.size[b];
    u.link[b] = a;
}
*/


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

using ll = long long;
using ld = long double;
#define MAX(type) numeric_limits<type>::max()
#define MIN(type) numeric_limits<type>::min()

/*
typedef struct dsu {
    vector<int> link;
    vector<int> size;
} dsu;
dsu init(int n);
int find(dsu& u, int x);
bool same(dsu& u, int a, int b);
void unite(dsu& u, int a, int b);
*/

/*
int modExp(int x, int n, int m);
int mmi(int x, int m);
*/

ll n;
ll m;
vector<vector<int>> adj;
vector<vector<bool>> matrix;

void solve() {
    cin >> n >> m;
    adj.resize(n + 1);
    matrix.resize(n + 1, vector<bool>(n + 1, false));
    for (int i = 0, a, b; i < m; i++) {
        cin >> a >> b;
        adj[a].push_back(b);
        matrix[a][b] = true;
    }

    ll p = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i != j) {
                ll sum = 0;
                for (auto v1 : adj[i]) {
                    if (matrix[v1][j]) {
                        sum++;
                    }
                }
                p += sum * (sum - 1) / 2;
            }
        }
    }

    cout << p << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    for (int i = 1; i <= t; i++) {
        solve();
    }
    return EXIT_SUCCESS;
}

/*
int modExp(const int x, const int n, const int m) {
    if (n == 0) {
        return 1 % m;
    }
    LLint u = modExp(x, n / 2, m);
    u = (u * u) % m;
    if (n % 2 == 1) {
        u = (u * x) % m;
    }
    return u;
}
int mmi(const int x, const int p) { return modExp(x, p - 2, p); }
*/

/*
dsu init(int n) {
    dsu u;
    u.link = vector<int>(n + 1);
    u.size = vector<int>(n + 1);
    for (int i = 1; i <= n; i++) {
        u.link[i] = i;
        u.size[i] = 1;
    }
    return u;
}

int find(dsu& u, int x) {
    while (x != u.link[x]) {
        x = u.link[x];
    }
    return x;
}

bool same(dsu& u, int a, int b) { return find(u, a) == find(u, b); }

void unite(dsu& u, int a, int b) {
    a = find(u, a);
    b = find(u, b);
    if (u.size[a] < u.size[b]) {
        swap(a, b);
    }
    u.size[a] += u.size[b];
    u.link[b] = a;
}
*/


#include<bits/stdc++.h>

using namespace std;
vector< vector<int> >lista(3000);
int main()
{
    int n, m;
    long long ans=0;
    scanf("%d%d", &n, &m);
    for(int i=0; i<m; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        x--; y--;
        lista[x].push_back(y);
    }
    for(int i=0; i<n; i++)
    {
        map<int,int>u;
        for(int j=0; j<lista[i].size(); j++)
            u[lista[i][j]]++;
        for(auto h=u.begin(); h!=u.end(); h++)
            ans+=(u->second)*(u->second)-(u->second);
        u.clear();
    }
    printf("%d\n", ans);
}


#include<bits/stdc++.h>

using namespace std;
vector< vector<int> >lista(3000);
int main()
{
    int n, m;
    long long ans=0;
    scanf("%d%d", &n, &m);
    for(int i=0; i<m; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        x--; y--;
        lista[x].push_back(y);
    }
    for(int i=0; i<n; i++)
    {
        map<int,int>u;
        for(int j=0; j<lista[i].size(); j++)
            for(int h=0; h<lista[lista[i][j]].size(); h++)
            u[lista[lista[i][j]][h]]++;
        for(auto h:u){
            if(h.first!=i){
            ans+=((h.second)*(h.second)-(h.second))>>1;
            //cout<<i<<": "<<h.first<<" "<<h.second<<endl;
            }
        }
        u.clear();
    }
    printf("%d\n", ans);
}


#include<bits/stdc++.h>

using namespace std;
vector< vector<int> >lista(3000);
int main()
{
    int n, m;
    long long ans=0;
    scanf("%d%d", &n, &m);
    for(int i=0; i<m; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        x--; y--;
        lista[x].push_back(y);
    }
    for(int i=0; i<n; i++)
    {
        map<int,int>u;
        for(int j=0; j<lista[i].size(); j++)
            for(int h=0; h<lista[lista[i][j]].size(); h++)
            u[lista[lista[i][j]][h]]++;
        for(auto h:u){
            ans+=((h.second)*(h.second)-(h.second))>>1;
        }
        u.clear();
    }
    printf("%d\n", ans);
}


#include<bits/stdc++.h>

using namespace std;
vector< vector<int> >lista(3000);
int main()
{
    int n, m;
    long long ans=0;
    scanf("%d%d", &n, &m);
    for(int i=0; i<m; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        x--; y--;
        lista[x].push_back(y);
    }
    for(int i=0; i<n; i++)
    {
        map<int,int>u;
        for(int j=0; j<lista[i].size(); j++)
            u[lista[i][j]]++;
        for(auto h:u)
            ans+=(h.second)*(h.second)-(h.second);
        u.clear();
    }
    printf("%d\n", ans);
}


#include<bits/stdc++.h>

using namespace std;
vector< vector<int> >lista(3000);
int main()
{
    int n, m;
    long long ans=0;
    scanf("%d%d", &n, &m);
    for(int i=0; i<m; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        x--; y--;
        lista[x].push_back(y);
    }
    for(int i=0; i<n; i++)
    {
        map<int,int>u;
        for(int j=0; j<lista[i].size(); j++)
            u[lista[i][j]]++;
        for(auto h:u)
            ans+=(h->second)*(h->second)-(h->second);
        u.clear();
    }
    printf("%d\n", ans);
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n,m;
    cin>>n>>m;
    vector<int> g[n];
    int mat[n][n]={0};
    for(int i=0;i<m;i++){
        int x,y;
        x--;y--;
        cin>>x>>y;
        g[x].push_back(y);
        //g[y].push_back(x);
        mat[x][y] = 1;
    }

	long long ans=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i==j){
				continue;
			}
			long long cnt=0;
			for(auto it : g[i]){
				if(mat[it][j]){
					cnt++;
				}
			}
			ans += cnt*(cnt-1)/2;
		}
	}
	cout<<ans;
    // long long ans = 0;

    // for(int i=0;i<n;i++){
    //     map<int,int> mp;
    //     for(int j=0;j<g[i].size();j++){
    //         mp[g[i][j]]++;
    //     }
    //     cout<<i<<' ';
    //     for(int j=0;j<n;j++){
    //         if(i == j)  continue;
    //         for(int k=0;k<g[j].size();k++){
    //             mp[g[j][k]]++;
    //         }
    //         cout<<j<<' ';
    //         int cnt=0;
    //         for(auto it : mp){

    //             if(it.second == 2){
    //                 cout<<it.first<<' ';
    //                 cnt++;
    //             }
    //         }
    //         for(int k=0;k<g[j].size();k++){
    //             mp[g[j][k]]--;
    //         }
    //         cout<<endl;
    //         ans += ((1LL*cnt*(cnt-1))/2);
    //     }
    //     cout<<endl;

    // }
    // cout<<ans;

}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n,m;
    cin>>n>>m;
    vector<int> g[n];
    int mat[n][n]={0};
    for(int i=0;i<m;i++){
        int x,y;
        x--;y--;
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
        mat[x][y] = 1;
    }

	long long ans=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i==j){
				continue;
			}
			long long cnt=0;
			for(auto it : g[i]){
				if(mat[it][j]){
					cnt++;
				}
			}
			ans += cnt*(cnt-1)/2;
		}
	}
	cout<<ans;
    // long long ans = 0;

    // for(int i=0;i<n;i++){
    //     map<int,int> mp;
    //     for(int j=0;j<g[i].size();j++){
    //         mp[g[i][j]]++;
    //     }
    //     cout<<i<<' ';
    //     for(int j=0;j<n;j++){
    //         if(i == j)  continue;
    //         for(int k=0;k<g[j].size();k++){
    //             mp[g[j][k]]++;
    //         }
    //         cout<<j<<' ';
    //         int cnt=0;
    //         for(auto it : mp){

    //             if(it.second == 2){
    //                 cout<<it.first<<' ';
    //                 cnt++;
    //             }
    //         }
    //         for(int k=0;k<g[j].size();k++){
    //             mp[g[j][k]]--;
    //         }
    //         cout<<endl;
    //         ans += ((1LL*cnt*(cnt-1))/2);
    //     }
    //     cout<<endl;

    // }
    // cout<<ans;

}


#include <bits/stdc++.h>

using namespace std;
int mat[3001][3001];

int main() {
    int n,m;
    cin>>n>>m;
    vector<int> g[n];
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        x--;y--;

        g[x].push_back(y);
        //g[y].push_back(x);
        mat[x][y] = 1;
    }

	long long ans=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i==j){
				continue;
			}
			long long cnt=0;
			for(auto it : g[i]){
				if(mat[it][j]){
					cnt++;
				}
			}
			ans += cnt*(cnt-1)/2;
		}
	}
	cout<<ans;
    // long long ans = 0;

    // for(int i=0;i<n;i++){
    //     map<int,int> mp;
    //     for(int j=0;j<g[i].size();j++){
    //         mp[g[i][j]]++;
    //     }
    //     cout<<i<<' ';
    //     for(int j=0;j<n;j++){
    //         if(i == j)  continue;
    //         for(int k=0;k<g[j].size();k++){
    //             mp[g[j][k]]++;
    //         }
    //         cout<<j<<' ';
    //         int cnt=0;
    //         for(auto it : mp){

    //             if(it.second == 2){
    //                 cout<<it.first<<' ';
    //                 cnt++;
    //             }
    //         }
    //         for(int k=0;k<g[j].size();k++){
    //             mp[g[j][k]]--;
    //         }
    //         cout<<endl;
    //         ans += ((1LL*cnt*(cnt-1))/2);
    //     }
    //     cout<<endl;

    // }
    // cout<<ans;

}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n,m;
    cin>>n>>m;
    vector<int> g[n];
    int mat[n][n]={0};
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        x--;y--;

        g[x].push_back(y);
        //g[y].push_back(x);
        mat[x][y] = 1;
    }

	long long ans=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i==j){
				continue;
			}
			long long cnt=0;
			for(auto it : g[i]){
				if(mat[it][j]){
					cnt++;
				}
			}
			ans += cnt*(cnt-1)/2;
		}
	}
	cout<<ans;
    // long long ans = 0;

    // for(int i=0;i<n;i++){
    //     map<int,int> mp;
    //     for(int j=0;j<g[i].size();j++){
    //         mp[g[i][j]]++;
    //     }
    //     cout<<i<<' ';
    //     for(int j=0;j<n;j++){
    //         if(i == j)  continue;
    //         for(int k=0;k<g[j].size();k++){
    //             mp[g[j][k]]++;
    //         }
    //         cout<<j<<' ';
    //         int cnt=0;
    //         for(auto it : mp){

    //             if(it.second == 2){
    //                 cout<<it.first<<' ';
    //                 cnt++;
    //             }
    //         }
    //         for(int k=0;k<g[j].size();k++){
    //             mp[g[j][k]]--;
    //         }
    //         cout<<endl;
    //         ans += ((1LL*cnt*(cnt-1))/2);
    //     }
    //     cout<<endl;

    // }
    // cout<<ans;

}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n,m;
    cin>>n>>m;
    vector<int> g[n];
    int mat[n][n]={0};
    for(int i=0;i<m;i++){
        int x,y;
        cin>>x>>y;
        x--;y--;

        g[x].push_back(y);
        g[y].push_back(x);
        mat[x][y] = 1;
    }

	long long ans=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i==j){
				continue;
			}
			long long cnt=0;
			for(auto it : g[i]){
				if(mat[it][j]){
					cnt++;
				}
			}
			ans += cnt*(cnt-1)/2;
		}
	}
	cout<<ans;
    // long long ans = 0;

    // for(int i=0;i<n;i++){
    //     map<int,int> mp;
    //     for(int j=0;j<g[i].size();j++){
    //         mp[g[i][j]]++;
    //     }
    //     cout<<i<<' ';
    //     for(int j=0;j<n;j++){
    //         if(i == j)  continue;
    //         for(int k=0;k<g[j].size();k++){
    //             mp[g[j][k]]++;
    //         }
    //         cout<<j<<' ';
    //         int cnt=0;
    //         for(auto it : mp){

    //             if(it.second == 2){
    //                 cout<<it.first<<' ';
    //                 cnt++;
    //             }
    //         }
    //         for(int k=0;k<g[j].size();k++){
    //             mp[g[j][k]]--;
    //         }
    //         cout<<endl;
    //         ans += ((1LL*cnt*(cnt-1))/2);
    //     }
    //     cout<<endl;

    // }
    // cout<<ans;

}


#include<iostream>
#include<vector>

#define max 3000+1
using namespace std;
int n, m, start, endd, o, counter;
vector<int>map[max];
int map2[max][max];

int main() {
	scanf_s("%d %d", &n, &m);
	while (m--) {
		scanf_s("%d %d", &start, &endd);
		map[start].push_back(endd);
		map2[start][endd] = 1;
	}
	for (auto i = 1; i <= n; i += 1) {
		for (auto j = 1; j <= n; j += 1) {
			if (i != j) {
				o = 0;
				for (auto x : map[i]) {
					if (map2[x][j] == 1) {
						o++;
					}
				}
				counter += o * (o - 1) / 2;
			}
		}
	}
	cout << counter;
}


#include<iostream>
#include<vector>
#define max 3000+1
using namespace std;
int n, m, start, endd, o, counter;
vector<int>map[max];
int main() {
	scanf_s("%d %d", &n, &m);
	while (m--) {
		scanf_s("%d %d", &start, &endd);
		map[start].push_back(endd);
	}
	for (auto i = 1; i <= n; i++) {
		for (auto j = 1; j <= n; j++) {
			if (j != i) {
				o = 0;
				for (auto x : map[i]) {
					for (auto y : map[x]) {
						if (y == j) {
							o++;
						}
					}
				}
				counter += o * (o - 1) / 2;
			}
		}
	}
	cout << counter;
}


#include <iostream>
#include <vector>
using namespace std;
int nxt[3001][3001];vector <int> adj[3001];
int visit[3001]= {};
long long int r=0;
long long int ans=0;

int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1; i<=m; i++)
    {
        int x,y;
        cin>>x>>y;
        nxt[x][y]=1;
        adj[x].push_back(y);

    }

    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
        {
            r=0;
            if(j==i)continue;
            for(int k=0; k<adj[i].size(); k++)
                if(adj[i][k] != i && adj[i][k] != j && nxt[adj[i][k]][j]==1 )
                {

                    r++;//cout<<i<<" "<<k<<" "<<j<<endl;
                }
            ans+=r*(r-1)/2;

        }
    cout<<ans;
}


#include <iostream>
#include <vector>
using namespace std;
int adj[3001][3001];
int visit[3001]= {};
long long int r=0;
long long int ans=0;

int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1; i<=m; i++)
    {
        int x,y;
        cin>>x>>y;
        adj[x][y]=1;

    }

    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
        {
            r=0;
            if(j==i)continue;
            for(int k=1; k<=n; k++)
                if(adj[i][k]==adj[k][j] && k!=i && k!=j && adj[i][k]==1 )
                {

                    r++;//cout<<i<<" "<<k<<" "<<j<<endl;
                }
            ans+=r*(r-1)/2;

        }
    cout<<ans;
}


#include <iostream>
#include <vector>
using namespace std;
vector <int> adj[3001];int visit[3001]={};long long int k=0;long long int ans=0;

void dfs(int a,int b)
{  k=0;
    for(int i=0;i<adj[a].size();i++)
    {
      if(adj[a][i]!=b)
      for(int j=0;j<adj[adj[a][i]].size();j++)
      {
            if(adj[adj[a][i]][j]==b)k++;
      }
    }
    return ;
}

int main()
{
    int n,m;cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y;cin>>x>>y;
        adj[x].push_back(y);

    }

    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    {if(j==i)continue;dfs(i,j);if(k>=2){ans=ans+k*(k-1)/2;cout<<k<<endl;}}
    cout<<ans;
}


#include <iostream>
#include <vector>
using namespace std;
vector <int> adj[3001];int visit[3001]={};long long int k=0;long long int ans=0;

void dfs(int a,int b)
{  k=0;
    for(int i=0;i<adj[a].size();i++)
    {
      if(adj[a][i]!=b)
      for(int j=0;j<adj[adj[a][i]].size();j++)
      {
            if(adj[adj[a][i]][j]==b)k++;
      }
    }
    return ;
}

int main()
{
    int n,m;cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y;cin>>x>>y;
        adj[x].push_back(y);

    }

    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    {if(j==i)continue;dfs(i,j);if(k>=2){ans=ans+k*(k-1)/2;}}
    cout<<ans;
}


#include <iostream>
#include <vector>
using namespace std;
vector <vector <int> > adjlist;
int arr[3001];
int n, m;
int a, b;
void dfs(int node, int dist, int par) {
	if (par != node && dist == 2) {
		arr[node]++;
		return;
	}
	else if (dist == 2)
		return;
	for (int i = 0; i < adjlist[node].size(); i++)
		dfs(adjlist[node][i], dist + 1, par);
}
int main() {
	cin >> n >> m;
	adjlist.resize(n + 1);
	for (int i = 0; i < m; i++) {
		cin >> a >> b;
		adjlist[a].push_back(b);
	}
	long long int ans = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j <= 3000; j++)
			arr[j] = 0;
		dfs(i, 0, i);
		for (int j = 0; j <= 3000; j++)
			ans += (arr[j]) * (arr[j] - 1) / 2;
	}
	cout << ans << '\n';
}


#include <sstream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cassert>
#include <ctime>
#include <stack>
#include <map>
#include <math.h>
#include <cstdio>
#include <set>
#include <deque>
#include <memory.h>
#include <queue>
#include <iomanip>

typedef long long ll;
typedef unsigned long long ull;
double EPS = 1E-9;
using namespace std;

struct node
{
	int par, ind;
};

bool BS(ll key)
{
	/*int high = vec.size()-1; int low = 0;
	while (low < high)
	{
		int mid = (low + high ) / 2;
		if(vec[mid] < key)
			low= mid +1;
		else
			high = mid;
	}
	if( key == vec[low] || key == vec[high])
		return true;*/
	return false;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif

	int  n, m;

	const int count = 30;//00;
	int a[count][count];
	int visited[count];
	int res[count][count];

	while(cin  >> n >> m)
	{
		memset(a,0,sizeof(a));
		memset(visited,0,sizeof(visited));
		memset(res,0,sizeof(res));
		int ind = -1;
		for(int i=0;i<m;i++)
		{
			int val1 , val2;
			cin >> val1 >> val2;
			a[val1-1][val2-1] = true;
		}
		queue<node> q; node f;f.par =-1; f.ind = 0;
		q.push(f);
		while(!q.empty())
		{
			f = q.front();
			node t; t.par = f.ind;
			visited[f.ind] = 1;
			for(int i=0; i<n;i++)
			{
				if(a[f.ind][i]&& f.ind != i)
				{
					if(f.par >=0 && f.par != i)
						res[f.par][i] ++;
					if(visited[i] ==1 )
					{
						for(int j=0;j<n;j++)
							if(a[i][j] && j != f.ind)
								res[f.ind][j]++;
					}
					else
					{
						t.ind = i;
						visited[i] = 1;
						q.push(t);
					}
				}
			}
			q.pop();
		}
		ll tot =0;
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
				tot += (res[i][j] * (res[i][j] - 1) /2);
		cout << tot << endl;
	}
#ifndef ONLINE_JUDGE
	system("pause");
#endif

	return 0;
}


#include <sstream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cassert>
#include <ctime>
#include <stack>
#include <map>
#include <math.h>
#include <cstdio>
#include <set>
#include <deque>
#include <memory.h>
#include <queue>
#include <iomanip>

typedef long long ll;
typedef unsigned long long ull;
double EPS = 1E-9;
using namespace std;

struct node
{
	int par, ind;
};

bool BS(ll key)
{
	/*int high = vec.size()-1; int low = 0;
	while (low < high)
	{
		int mid = (low + high ) / 2;
		if(vec[mid] < key)
			low= mid +1;
		else
			high = mid;
	}
	if( key == vec[low] || key == vec[high])
		return true;*/
	return false;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif

	int  n, m;

	const int count = 30;//00;
	int a[count][count];
	int visited[count];
	int res[count][count];

	while(cin  >> n >> m)
	{
		memset(a,0,sizeof(a));
		memset(visited,0,sizeof(visited));
		memset(res,0,sizeof(res));
		int ind = -1;
		for(int i=0;i<m;i++)
		{
			int val1 , val2;
			cin >> val1 >> val2;
			a[val1-1][val2-1] = true;
		}
		bool found = true;
		while(found)
		{
			queue<node> q;node f;
			found = false;
			for(int i=0;i<n;i++)
			{
				if(visited[i] == 0)
				{
					 f.par =-1; f.ind = i;
					q.push(f);
					found = true;
					break;
				}
			}

			while(!q.empty())
			{
				f = q.front();
				node t; t.par = f.ind;
				visited[f.ind] = 1;
				for(int i=0; i<n;i++)
				{
					if(a[f.ind][i]&& f.ind != i)
					{
						if(f.par >=0 && f.par != i)
							res[f.par][i] ++;
						if(visited[i] ==1 )
						{
							for(int j=0;j<n;j++)
								if(a[i][j] && j != f.ind)
									res[f.ind][j]++;
						}
						else
						{
							t.ind = i;
							visited[i] = 1;
							q.push(t);
						}
					}
				}
				q.pop();
			}
		}
		ll tot =0;
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
				tot += (res[i][j] * (res[i][j] - 1)) /2;
		cout << tot << endl;
	}
#ifndef ONLINE_JUDGE
	system("pause");
#endif

	return 0;
}


#include <sstream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cassert>
#include <ctime>
#include <stack>
#include <map>
#include <math.h>
#include <cstdio>
#include <set>
#include <deque>
#include <memory.h>
#include <queue>
#include <iomanip>

typedef long long ll;
typedef unsigned long long ull;
double EPS = 1E-9;
using namespace std;

struct node
{
	int par, ind;
};

bool BS(ll key)
{
	/*int high = vec.size()-1; int low = 0;
	while (low < high)
	{
		int mid = (low + high ) / 2;
		if(vec[mid] < key)
			low= mid +1;
		else
			high = mid;
	}
	if( key == vec[low] || key == vec[high])
		return true;*/
	return false;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif

	int  n, m;

	const int count = 3000;
	int a[count][count];
	int visited[count];
	int res[count][count];

	while(cin  >> n >> m)
	{
		memset(a,0,sizeof(a));
		memset(visited,0,sizeof(visited));
		memset(res,0,sizeof(res));
		int ind = -1;
		for(int i=0;i<m;i++)
		{
			int val1 , val2;
			cin >> val1 >> val2;
			a[val1-1][val2-1] = true;
		}
		bool found = true;
		while(found)
		{
			queue<node> q;node f;
			found = false;
			for(int i=0;i<n;i++)
			{
				if(visited[i] == 0)
				{
					 f.par =-1; f.ind = i;
					q.push(f);
					found = true;
					break;
				}
			}

			while(!q.empty())
			{
				f = q.front();
				node t; t.par = f.ind;
				visited[f.ind] = 1;
				for(int i=0; i<n;i++)
				{
					if(a[f.ind][i]&& f.ind != i)
					{
						if(f.par >=0 && f.par != i)
							res[f.par][i] ++;
						if(visited[i] ==1 )
						{
							for(int j=0;j<n;j++)
								if(a[i][j] && j != f.ind)
									res[f.ind][j]++;
						}
						else
						{
							t.ind = i;
							visited[i] = 1;
							q.push(t);
						}
					}
				}
				q.pop();
			}
		}
		ll tot =0;
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
				tot += (res[i][j] * (res[i][j] - 1)) /2;
		cout << tot << endl;
	}
#ifndef ONLINE_JUDGE
	system("pause");
#endif

	return 0;
}


#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

vector <int> q[3111];
int d[3333][3333];

long long com(int n){
	long long ans = 1;
	ans = n * (n - 1);
	ans /= 2;
	return ans;
}
int main(){

	int n, m;
	scanf("%d %d", &n, &m);

	for (int i = 0; i < m; i++){
		int x, y;
		scanf("%d %d", &x, &y);
		q[x].push_back(y);
	}

	for (int i = 1; i <= n; i++){
		for (int j = 0; j < q[i].size(); j++){
			for (int k = 0; k < q[q[i][j]].size(); k++){
				d[i][q[q[i][j]][k]] += 1;
			}
		}
	}

	long long ans = 0;
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= n; j++){
			if (i != j) {
				ans += com(d[i][j]);
			}
		}
	}

	printf("%d", ans);

}


#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

vector <int> q[3111];
int d[3333][3333];

long long com(int n){
	long long ans = 1;
	ans = n * n - 1;
	ans /= 2;
	return ans;
}
int main(){

	int n, m;
	scanf("%d %d", &n, &m);

	for (int i = 0; i < m; i++){
		int x, y;
		scanf("%d %d", &x, &y);
		q[x].push_back(y);
	}

	for (int i = 1; i <= n; i++){
		for (int j = 0; j < q[i].size(); j++){
			for (int k = 0; k < q[q[i][j]].size(); k++){
				d[i][q[q[i][j]][k]] += 1;
			}
		}
	}

	long long ans = 0;
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= n; j++){
			if (i != j) {
				ans += com(d[i][j]);
			}
		}
	}

	printf("%d", ans);

}


#include <cstdint>
#include <cstdio>
#include <iostream>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>

using namespace std;

class Node
{
public:
	Node() :
		id		( 0 ),
		road	(),
		r		( 0 )
	{}

	uint32_t id;
	vector< Node* > road;
	uint32_t r;
};

uint32_t main()
{
	uint32_t n, m;
	cin >> n >> m;

	vector< Node > node( n );
	for( uint32_t i = 0; i < n; i++ )
	{
		node[i].id = i + 1;
	}

	for( uint32_t i = 0; i < m; i++ )
	{
		uint32_t a, b;
		cin >> a >> b;
		node[a-1].road.push_back( &node[b-1] );
	}

	uint32_t answer = 0;
	for( uint32_t i = 0; i < n; i++ )
	{
		for( uint32_t j = 0; j < node[i].road.size(); j++ )
		{
			for( uint32_t k = 0; k < node[i].road[j]->road.size(); k++ )
			{
				if( node[i].road[j]->road[k]->id != node[i].id )
				{
					answer += node[i].road[j]->road[k]->r;
					node[i].road[j]->road[k]->r++;
				}
			}
		}
		for( uint32_t j = 0; j < node[i].road.size(); j++ )
		{
			for( uint32_t k = 0; k < node[i].road[j]->road.size(); k++ )
			{
				node[i].road[j]->road[k]->r = 0;
			}
		}
	}
	cout << answer;

	return EXIT_SUCCESS;
}


#include <cstdint>
#include <cstdio>
#include <iostream>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>

using namespace std;

class Node
{
public:
	Node() :
		road(),
		from( 0 )
	{}

	uint32_t id;
	vector< Node* > road;
	uint32_t from;
};

class Answer
{
public:
	Answer( uint32_t a, uint32_t b, uint32_t c, uint32_t d ) :
		a( a ),
		b( b ),
		c( c ),
		d( d )
	{}

	bool operator==( const Answer& answer ) const
	{
		return a == answer.a && b == answer.b && c == answer.c && d == answer.d;
	}

	uint32_t a;
	uint32_t b;
	uint32_t c;
	uint32_t d;
};

uint32_t main()
{
	uint32_t n, m;
	cin >> n >> m;

	vector< Node > node( n );
	for( uint32_t i = 0; i < n; i++ )
	{
		node[i].id = i + 1;
	}

	for( uint32_t i = 0; i < m; i++ )
	{
		uint32_t a, b;
		cin >> a >> b;
		node[b-1].road.push_back( &node[a-1] );
	}

	//vector< Answer > answer;
	uint32_t answer = 0;
	for( uint32_t i = 0; i < n; i++ )
	{
		vector< Node* >& roads = node[i].road;
		if( roads.size() >= 2 )
		{
			for( uint32_t j = 0; j < roads.size() - 1; j++ )
			{
				for( uint32_t k = j + 1; k < roads.size(); k++ )
				{
					//if( j != k )
					{
						for( uint32_t p = 0; p < roads[j]->road.size(); p++ )
						{
							roads[j]->road[p]->from = roads[j]->id;
						}
						for( uint32_t p = 0; p < roads[k]->road.size(); p++ )
						{
							if( roads[k]->road[p]->id != node[i].id && roads[k]->road[p]->from )
							{
								uint32_t from1 = roads[k]->road[p]->from;
								uint32_t from2 = roads[k]->id;
								if( from1 > from2 )
								{
									swap( from1, from2 );
								}
								//Answer t( node[i].id, from1, from2, roads[k]->road[p]->id );
								//if( find_if( answer.begin(), answer.end(), [&t]( const Answer& a ){ return a == t; } ) == answer.end() )
								{
									//answer.push_back( t );
											answer++;
								}
							}
						}
						for( uint32_t p = 0; p < roads[j]->road.size(); p++ )
						{
							roads[j]->road[p]->from = 0;
						}
					}
				}
			}
		}
	}
	//cout << answer.size();
	cout << answer;

	return EXIT_SUCCESS;
}


#include <cstdint>
#include <cstdio>
#include <iostream>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>

using namespace std;

class Node
{
public:
	Node() :
		road(),
		from( 0 )
	{}

	uint32_t id;
	vector< Node* > road;
	uint32_t from;
};

class Answer
{
public:
	Answer( uint32_t a, uint32_t b, uint32_t c, uint32_t d ) :
		a( a ),
		b( b ),
		c( c ),
		d( d )
	{}

	bool operator==( const Answer& answer ) const
	{
		return a == answer.a && b == answer.b && c == answer.c && d == answer.d;
	}

	uint32_t a;
	uint32_t b;
	uint32_t c;
	uint32_t d;
};

uint32_t main()
{
	uint32_t n, m;
	cin >> n >> m;

	vector< Node > node( n );
	for( uint32_t i = 0; i < n; i++ )
	{
		node[i].id = i + 1;
	}

	for( uint32_t i = 0; i < m; i++ )
	{
		uint32_t a, b;
		cin >> a >> b;
		node[b-1].road.push_back( &node[a-1] );
	}

	vector< Answer > answer;
	for( uint32_t i = 0; i < n; i++ )
	{
		vector< Node* >& roads = node[i].road;
		if( roads.size() >= 2 )
		{
			for( uint32_t j = 0; j < roads.size() - 1; j++ )
			{
				for( uint32_t k = j + 1; k < roads.size(); k++ )
				{
					//if( j != k )
					{
						for( uint32_t p = 0; p < roads[j]->road.size(); p++ )
						{
							roads[j]->road[p]->from = roads[j]->id;
						}
						for( uint32_t p = 0; p < roads[k]->road.size(); p++ )
						{
							if( roads[k]->road[p]->id != node[i].id && roads[k]->road[p]->from )
							{
								uint32_t from1 = roads[k]->road[p]->from;
								uint32_t from2 = roads[k]->id;
								if( from1 > from2 )
								{
									swap( from1, from2 );
								}
								Answer t( node[i].id, from1, from2, roads[k]->road[p]->id );
								if( find_if( answer.begin(), answer.end(), [&t]( const Answer& a ){ return a == t; } ) == answer.end() )
								{
									answer.push_back( t );
								}
							}
						}
						for( uint32_t p = 0; p < roads[j]->road.size(); p++ )
						{
							roads[j]->road[p]->from = 0;
						}
					}
				}
			}
		}
	}
	cout << answer.size();

	return EXIT_SUCCESS;
}


#include <cstdint>
#include <cstdio>
#include <iostream>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>

using namespace std;

class Node
{
public:
	Node() :
		road(),
		from( 0 )
	{}

	uint32_t id;
	vector< Node* > road;
	uint32_t from;
};

class Answer
{
public:
	Answer( uint32_t a, uint32_t b, uint32_t c, uint32_t d ) :
		a( a ),
		b( b ),
		c( c ),
		d( d )
	{}

	bool operator==( const Answer& answer ) const
	{
		return a == answer.a && b == answer.b && c == answer.c && d == answer.d;
	}

	uint32_t a;
	uint32_t b;
	uint32_t c;
	uint32_t d;
};

uint32_t main()
{
	uint32_t n, m;
	cin >> n >> m;

	vector< Node > node( n );
	for( uint32_t i = 0; i < n; i++ )
	{
		node[i].id = i + 1;
	}

	for( uint32_t i = 0; i < m; i++ )
	{
		uint32_t a, b;
		cin >> a >> b;
		node[b-1].road.push_back( &node[a-1] );
	}

	vector< Answer > answer;
	for( uint32_t i = 0; i < n; i++ )
	{
		vector< Node* >& roads = node[i].road;
		if( roads.size() >= 2 )
		{
			for( uint32_t j = 0; j < roads.size(); j++ )
			{
				for( uint32_t k = 0; k < roads.size(); k++ )
				{
					if( j != k )
					{
						for( uint32_t p = 0; p < roads[j]->road.size(); p++ )
						{
							roads[j]->road[p]->from = roads[j]->id;
						}
						for( uint32_t p = 0; p < roads[k]->road.size(); p++ )
						{
							if( roads[k]->road[p]->id != node[i].id && roads[k]->road[p]->from )
							{
								uint32_t from1 = roads[k]->road[p]->from;
								uint32_t from2 = roads[k]->id;
								if( from1 > from2 )
								{
									swap( from1, from2 );
								}
								Answer t( node[i].id, from1, from2, roads[k]->road[p]->id );
								if( find_if( answer.begin(), answer.end(), [&t]( const Answer& a ){ return a == t; } ) == answer.end() )
								{
									answer.push_back( t );
								}
							}
						}
						for( uint32_t p = 0; p < roads[j]->road.size(); p++ )
						{
							roads[j]->road[p]->from = 0;
						}
					}
				}
			}
		}
	}
	cout << answer.size();

	return EXIT_SUCCESS;
}


#include <cstdint>
#include <cstdio>
#include <iostream>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>

using namespace std;

class Node
{
public:
	Node() :
		id	( 0 ),
		in	(),
		out	()
	{}

	uint32_t id;
	vector< Node* > out;
	vector< Node* > in;
};

class Key
{
public:
	Key( uint32_t a, uint32_t b ) :
		a( a ),
		b( b )
	{}

	bool operator==( const Key& key ) const
	{
		return ( a == key.a && b == key.b ) || ( a == key.b && b == key.a );
	}

	bool operator<( const Key& key ) const
	{
		return ( a < key.a ? true : ( a == key.a ? ( b < key.b ) : false ) );
	}

	uint32_t a;
	uint32_t b;
};

uint32_t main()
{
	uint32_t n, m;
	cin >> n >> m;

	vector< Node > node( n );
	for( uint32_t i = 0; i < n; i++ )
	{
		node[i].id = i + 1;
	}

	for( uint32_t i = 0; i < m; i++ )
	{
		uint32_t a, b;
		cin >> a >> b;
		node[a-1].out.push_back( &node[b-1] );
		node[b-1].in.push_back( &node[a-1] );
	}

	multimap< Key, uint32_t > r;
	for( uint32_t i = 0; i < n; i++ )
	{
		if( !node[i].in.empty() && !node[i].out.empty() )
		{
			for( uint32_t j = 0; j < node[i].in.size(); j++ )
			{
				for( uint32_t k = 0; k < node[i].out.size(); k++ )
				{
					if( node[i].in[j]->id != node[i].out[k]->id )
					{
						Key key( node[i].in[j]->id, node[i].out[k]->id );
						r.insert( make_pair( key, node[i].id ) );
					}
				}
			}
		}
	}
	cout << r.size() / 2;

	return EXIT_SUCCESS;
}


#include <cstdint>
#include <cstdio>
#include <iostream>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>

using namespace std;

class Node
{
public:
	Node() :
		road(),
		from( 0 )
	{}

	uint32_t id;
	vector< Node* > road;
	uint32_t from;
};

class Answer
{
public:
	Answer( uint32_t a, uint32_t b, uint32_t c, uint32_t d ) :
		a( a ),
		b( b ),
		c( c ),
		d( d )
	{}

	bool operator==( const Answer& answer ) const
	{
		return a == answer.a && b == answer.b && c == answer.c && d == answer.d;
	}

	uint32_t a;
	uint32_t b;
	uint32_t c;
	uint32_t d;
};

uint32_t main()
{
	uint32_t n, m;
	cin >> n >> m;

	vector< Node > node( n );
	for( uint32_t i = 0; i < n; i++ )
	{
		node[i].id = i + 1;
	}

	for( uint32_t i = 0; i < m; i++ )
	{
		uint32_t a, b;
		cin >> a >> b;
		node[b-1].road.push_back( &node[a-1] );
	}

	vector< Answer > answer;
	for( uint32_t i = 0; i < n; i++ )
	{
		vector< Node* >& roads = node[i].road;
		if( roads.size() >= 2 )
		{
			for( uint32_t j = 0; j < roads.size() - 1; j++ )
			{
				for( uint32_t k = j + 1; k < roads.size(); k++ )
				{
					//if( j != k )
					{
						for( uint32_t p = 0; p < roads[j]->road.size(); p++ )
						{
							roads[j]->road[p]->from = roads[j]->id;
						}
						for( uint32_t p = 0; p < roads[k]->road.size(); p++ )
						{
							if( roads[k]->road[p]->id != node[i].id && roads[k]->road[p]->from )
							{
								uint32_t from1 = roads[k]->road[p]->from;
								uint32_t from2 = roads[k]->id;
								if( from1 > from2 )
								{
									swap( from1, from2 );
								}
								Answer t( node[i].id, from1, from2, roads[k]->road[p]->id );
								//if( find_if( answer.begin(), answer.end(), [&t]( const Answer& a ){ return a == t; } ) == answer.end() )
								{
									answer.push_back( t );
								}
							}
						}
						for( uint32_t p = 0; p < roads[j]->road.size(); p++ )
						{
							roads[j]->road[p]->from = 0;
						}
					}
				}
			}
		}
	}
	cout << answer.size();

	return EXIT_SUCCESS;
}


#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<stdlib.h>
#include<iterator>
#include<iostream>
#include<numeric>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<stdio.h>
#include<fstream>
#include<vector>
#include<bitset>
#include<string>
#include<math.h>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>

using namespace std;

#define all(v)             v.begin(),v.end()
#define sz(v)            ((int)((v).size()))
#define psh(x)                  push_back(x)
#define sc(x)                 scanf("%d",&x)
#define sc2(x,y)         scanf("%d%d",&x,&y)
#define scl(x)              scanf("%lld",&x)
#define lop(i,n)        for(int i=0;i<n;++i)
#define loop(i,f,l)     for(int i=f;i<l;++i)
#define R_(s)         freopen(s, "r", stdin)
#define W_(s)        freopen(s, "w", stdout)
#define R_W R_("input.txt"),W_("output.txt")
#define PI           acos(-1)
#define INF             1<<30
#define DFS_GRAY           -1
#define DFS_WHITE           0
#define DFS_BLACK           1

typedef string            ss;
typedef long long         ll;
typedef pair <int, int>   ii;
typedef pair<ss, int>     si;
typedef pair<int, ss>     is;
typedef pair<char, int>  chi;
typedef pair<ss, ss>     pss;
typedef vector<ii>       vii;
typedef vector<int>       vi;
typedef vector<vi>       vvi;
typedef vector<ss>        vs;
typedef vector<ll>       vll;
typedef vector<vll>     vvll;

ll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll gcd(ll a, ll b)  { return (b == 0 ? a : gcd(b, a % b)); }
ll sd(ll x)  { return x<10 ? x : x % 10 + sd(x / 10); }
ll lcm(ll a, ll b){ return ((a*b) / gcd(a, b)); }

int n, m, u, v, out = 0, arr[3100][3100],t;
int main()
{

	vi adjlist[3100];
	sc2(n, m);
	lop(i, m)
	{
		sc2(u, v);
		adjlist[--u].psh(--v);
		arr[u][v] = 1;
	}
	lop(a, n)lop(c, n)if (a != c)
	{
		t = 0;
		lop(i, sz(adjlist[a]))if (arr[adjlist[a][i]][c])++t;
		out += (t - 1)*t / 2;
	}
	cout << out;
}


#include <stdio.h>
#include <memory.h>
#include <stdlib.h>

#define get_2d_element(r, a, b) ((r)[(a)][(b)])
#define begin_at(r, i) &get_2d_element(r,i,0)
#define next_at(r, i) get_2d_element(r,i, ++get_2d_element(r,i,0))

static int goin[3100][3100];
static int goout[3100][3100];
static int result[3100][3100];

int main()
{
	int m, n;
	scanf("%d%d", &n, &m);

	int ret = 0;

	while (m--)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		next_at(goout, a) = b;
	}

	int i;
	for (i = 1; i <= n; ++i)
	{
		int* it = begin_at(goout, i);
		while (*++it)
		{
			int* it2 = begin_at(goout, *it);
			while (*++it2)
			{
				if (*it2 != i)
				{
					ret += get_2d_element(result, i, *it2)++;
				}
			}
		}
	}

	printf("%d", ret);

	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<iomanip>
#include<cmath>
#include<sstream>
#include<iterator>

using namespace std;

typedef long long ll;

ll kol;

vector <vector <ll> > g;
vector <ll> used;

ll n, m;

void bfs(ll u){
	ll v;
	used.assign(n, 0);
	used[u]=3;
	for (int i=0; i<g[u].size(); i++){
		v=g[u][i];
		for (int j=0; j<g[v].size(); j++)
			used[g[v][j]]++;
	}
	for (int i=0; i<n; i++)
		if (used[i]==2)
			kol++;

}

int main(){
cin>>n>>m;
ll u, v;
kol=0;
g.resize(n);
used.resize(n, 0);
while (m--){
	cin>>u>>v;
	g[u-1].push_back(v-1);
}
for (int i=0; i<n; i++)
	bfs(i);
cout<<kol;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<iomanip>
#include<cmath>
#include<sstream>
#include<iterator>

using namespace std;

typedef long long ll;

ll kol;

vector <vector <ll> > g;
vector <ll> used;

ll n, m;

void bfs(ll u){
	ll v;
	used.assign(n, 0);
	used[u]=-100000000;
	for (int i=0; i<g[u].size(); i++){
		v=g[u][i];
		for (int j=0; j<g[v].size(); j++)
				used[g[v][j]]++;
	}
	for (int i=0; i<n; i++)
		if (used[i]>=2)
			kol++;

}

int main(){
cin>>n>>m;
ll u, v;
kol=0;
g.resize(n);
used.resize(n, 0);
while (m--){
	cin>>u>>v;
	g[u-1].push_back(v-1);
}
for (int i=0; i<n; i++)
	bfs(i);
cout<<kol;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<iomanip>
#include<cmath>
#include<sstream>
#include<iterator>

using namespace std;

typedef long long ll;

int kol;

vector <vector <int> > g;
vector <int> d;

int n, m;

void bfs(int u){
	int v;
	vector <int> used(n);
	for (int i=0; i<g[u].size(); i++){
		v=g[u][i];
		for (int j=0; j<g[v].size(); j++)
			if (g[v][j]!=u) used[g[v][j]]++;
	}
	for (int i=0; i<n; i++)
		kol+=used[i]*(used[i]-1)/2;
}

int main(){
cin>>n>>m;
int u, v;
g.resize(n);
d.assign(n, 0);
while (m--){
	cin>>u>>v;
	g[u-1].push_back(v-1);
}

for (int i=0; i<n; i++){
	bfs(i);
}
cout<<kol;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<iomanip>
#include<cmath>
#include<sstream>
#include<iterator>

using namespace std;

typedef long long ll;

ll kol;

vector <vector <ll> > g;
vector <ll> used;

ll n, m;

void bfs(ll u){
	ll v;
	used.assign(n, 0);
	used[u]=3;
	for (int i=0; i<g[u].size(); i++){
		v=g[u][i];
		for (int j=0; j<g[v].size(); j++)
			if (used[g[v][j]]<2)
				used[g[v][j]]++;
	}
	for (int i=0; i<n; i++)
		if (used[i]==2)
			kol++;

}

int main(){
cin>>n>>m;
ll u, v;
kol=0;
g.resize(n);
used.resize(n, 0);
while (m--){
	cin>>u>>v;
	g[u-1].push_back(v-1);
}
for (int i=0; i<n; i++)
	bfs(i);
cout<<kol;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<iomanip>
#include<cmath>
#include<sstream>
#include<iterator>

using namespace std;

typedef long long ll;

int kol;

vector <vector <int> > g;
vector <int> used;
vector <int> d;

int n, m;

void bfs(int u){
	int v;
	used.assign(n, 0);
	used[u]=3;

	for (int i=0; i<g[u].size(); i++){
		v=g[u][i];
		for (int j=0; j<g[v].size(); j++)
			used[g[v][j]]++;
			kol++;
	}
}

int main(){
cin>>n>>m;
int u, v;
g.resize(n);
d.assign(n, 0);
used.resize(n, 0);
while (m--){
	cin>>u>>v;
	g[u-1].push_back(v-1);
}
int ans=0;
for (int i=0; i<n; i++){
	kol=0;
	bfs(i);
	ans+=kol*(kol-1)/2;
}
cout<<ans;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<iomanip>
#include<cmath>
#include<sstream>
#include<iterator>

using namespace std;

typedef long long ll;

int kol;

vector <vector <int> > g;
vector <int> used;

int n, m;

void bfs(int u){
	int v;
	used.assign(n, 0);
	used[u]=3;
	vector <int> a;
	for (int i=0; i<g[u].size(); i++){
		v=g[u][i];
		for (int j=0; j<g[v].size(); j++)
			used[g[v][j]]++;
	}
	for (int i=0; i<n; i++)
		if (used[i]==2)
			kol++;

}

int main(){
cin>>n>>m;
int u, v;
g.resize(n);
used.resize(n, 0);
while (m--){
	cin>>u>>v;
	g[u-1].push_back(v-1);
}
for (int i=0; i<n; i++)
	bfs(i);
cout<<kol;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<iomanip>
#include<cmath>
#include<sstream>
#include<iterator>

using namespace std;

typedef long long ll;

int kol;

vector <vector <int> > g;
vector <int> d;

int n, m;

void bfs(int u){
	int v;
	vector <int> used(n);
	for (int i=0; i<g[u].size(); i++){
		v=g[u][i];
		for (int j=0; j<g[v].size(); j++)
			used[g[v][j]]++;
	}
	for (int i=0; i<n; i++)
		kol+=used[i]*(used[i]-1)/2;
}

int main(){
cin>>n>>m;
int u, v;
g.resize(n);
d.assign(n, 0);
while (m--){
	cin>>u>>v;
	g[u-1].push_back(v-1);
}

for (int i=0; i<n; i++){
	bfs(i);
}
cout<<kol;
}


//
//  main.cpp
//  #277.5-D
//
//  Created by KJBS2 on 2014. 12. 1..
//  Copyright (c) 2014년 KJBS2. All rights reserved.
//

#include <stdio.h>
#include <vector>

using namespace std;

#define MAX_N 3333

int N, M;
vector<int> V[MAX_N];

int Check[MAX_N];
int Cnt[MAX_N];

long long Ans;

void Get(int k) {
    Check[k] = k;
    for(int i=1; i<=N; i++) Cnt[i] = 0;
    for(int i=0; i<V[k].size(); i++) {
        int now = V[k][i];
        for(int j=0; j<V[now].size(); j++) {
            int next = V[now][j];
            Cnt[next]++;
        }
    }
    for(int i=1; i<=N; i++) {
        if(i == k) continue;
        Ans += (long long)(Cnt[i]-1)*(Cnt[i])/2;
    }
}
int main() {
    scanf("%d%d", &N, &M);
    for(int i=0; i<M; i++) {
        int x, y; scanf("%d%d", &x, &y);
        V[x].push_back(y);
    }

    for(int i=1; i<=N; i++)
        Get(i);

    printf("%I64d", Ans);
    return 0;
}


#include <iostream>
#include <utility>
#include <vector>
#include <cstdlib>

using namespace std;

int n, m;
int edge_out[3001][3001];
int len_edge_out[3001];

int edge_in[3001][3001];
int len_edge_in[3001];

int cmp(const void *p, const void *q) {
	return *((const int *)p) - *((const int *)q);
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		len_edge_out[i] = len_edge_in[i] = 0;
	while (m--) {
		int a, b;
		cin >> a >> b;
		edge_out[a][len_edge_out[a]++] = b;
		edge_in[b][len_edge_in[b]++] = a;
	}
	for (int i = 1; i <= n; i++) {
		qsort(edge_out[i], len_edge_out[i], sizeof(int), cmp);
		qsort(edge_in[i], len_edge_in[i], sizeof(int), cmp);
	}
	long long res = 0;
	for (int a = 1; a <= n; a++)
		for (int b = 1; b <= n; b++) {
			if (a == b)
				continue;
			long long size = 0;
			int p = 0, q = 0;
			while (p < len_edge_out[a] && q < len_edge_in[b]) {
				if (edge_out[a][p] == edge_in[b][q]) {
					size++;
					p++;
					q++;
				} else if (edge_out[a][p] < edge_in[b][q])
					p++;
				else
					q++;
			}
			if (size >= 2)
				res += size * (size - 1) / 2;
		}
	cout << res << endl;
	return 0;
}


#include<iostream>
#include<string>
#include<fstream>
#include<ostream>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iomanip>
#include <utility>
#include<sstream>
#include<map>
#include<unordered_map>
#include<set>
#include<queue>
#include<list>

#define pb push_back
#define mp make_pair
#define LL long long
#define ULL unsigned long long
#define PI 3.14159265
#define MOD 1000000007
using namespace std;

unsigned long long dp[2001][2001];
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);

   int n,m,x,y;
   ULL ans=0;

   cin>>n>>m;

  vector<int> v[3001];
  for(int i=0;i<m;i++)
  {
      cin>>x>>y;
      v[x].pb(y);
  }

  for(int i=1;i<=n;i++)
  {
      unordered_map<int,int> w;
      int l= v[i].size();
          //cout<<i<<":"<<l<<endl;
      for(int j=0;j<l;j++)
      {
          int z=v[i][j];
          int l1=v[z].size();
           //cout<<l1<<endl;
          for(int k=0;k<l1;k++)
          {
              if(v[z][k] !=i)
              {
                  ans += w[v[z][k]];
                  w[v[z][k]]++;
              }
          }
      }
  }

  cout<<ans;

    return 0;
}


#include <iostream>
#include <fstream>
#include <queue>
#include <limits.h>
#include <math.h>
#include <vector>
#include <string>
#include <algorithm>
#include <map>

#define endl "\n"
using namespace std;

#define bigsize  2000001

class Point{
public:
	int x;
	int y;
	Point(){
		this->x = -1; this->y=-1;
	}
	Point(int x, int y){
		this->x = x; this->y=y;
	}
	friend bool operator == (const Point &p1, const Point &p2) {
		return p1.x == p2.x && p1.y == p2.y;
	}
	friend bool operator != (const Point &p1, const Point &p2) {
		return p1.x != p2.x || p1.y != p2.y;
	}
};

class Zased{
public:
	long long a;
	long long b;
	int n;
	Zased(long long a, long long b,int n){
		this->a = a; this->b=b; this->n=n;
	}
	friend bool operator < (const Zased &p1, const Zased &p2) {
		return p1.a < p2.a;
	}
};

vector<vector<int> > g(3000);
vector<int> col(3000);

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

	int a,b;
	for(int i=0;i<m;i++){
		cin>>a;
		cin>>b;
		g[a-1].push_back(b-1);
	}

	long long res = 0;

	for(int i=0;i<n;i++){
		col.assign(n,0);
		for(int j=0;j<g[i].size();j++){
			int cur = g[i][j];
			for(int k=0;k<g[cur].size();k++){
				if(g[cur][k]==i) continue;
				col[g[cur][k]]++;
			}
		}
		for(int j=0;j<n;j++){
			res+=col[j]*(col[j]-1)/2;
		}
	}

	cout<<res;
}

int main(){

	#ifdef ILEASILE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#else
		ios_base::sync_with_stdio(false);
		//freopen("input.txt","r",stdin);
		//freopen("output.txt","w",stdout);
	#endif

	task();

	#ifdef ILEASILE
		fclose(stdin);
		fclose(stdout);
	#endif
	return 0;
}


#include <iostream>
#include <fstream>
#include <queue>
#include <limits.h>
#include <math.h>
#include <vector>
#include <string>
#include <algorithm>
#include <map>

#define endl "\n"
using namespace std;

#define bigsize  2000001

class Point{
public:
	int x;
	int y;
	Point(){
		this->x = -1; this->y=-1;
	}
	Point(int x, int y){
		this->x = x; this->y=y;
	}
	friend bool operator == (const Point &p1, const Point &p2) {
		return p1.x == p2.x && p1.y == p2.y;
	}
	friend bool operator != (const Point &p1, const Point &p2) {
		return p1.x != p2.x || p1.y != p2.y;
	}
};

class Zased{
public:
	long long a;
	long long b;
	int n;
	Zased(long long a, long long b,int n){
		this->a = a; this->b=b; this->n=n;
	}
	friend bool operator < (const Zased &p1, const Zased &p2) {
		return p1.a < p2.a;
	}
};

vector<vector<int> > g(3000);
vector<int> col(3000);

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

	int a,b;
	for(int i=0;i<m;i++){
		cin>>a;
		cin>>b;
		g[a-1].push_back(b-1);
	}

	int res = 0;

	for(int i=0;i<n;i++){
		col.assign(n,0);
		for(int j=0;j<g[i].size();j++){
			int cur = g[i][j];
			for(int k=0;k<g[cur].size();k++){
				if(g[cur][k]==i) continue;
				if(col[g[cur][k]]==1) res++;
				else col[g[cur][k]]++;
			}
		}
	}

	cout<<res;
}

int main(){

	#ifdef ILEASILE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#else
		ios_base::sync_with_stdio(false);
		//freopen("input.txt","r",stdin);
		//freopen("output.txt","w",stdout);
	#endif

	task();

	#ifdef ILEASILE
		fclose(stdin);
		fclose(stdout);
	#endif
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
#include<iostream>
#include<string>
#define mem(a,b) memset(a,b,sizeof(a))
#define LL __int64
#define MAX 3010
using namespace std;

bool flag[MAX][MAX];
vector<int> v[MAX];

int main()
{
    int n ,m;
    int a ,b;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i = 0;i < m;i++)
        {
            scanf("%d%d",&a,&b);
            if(!flag[a][b])
            {
                flag[a][b] = 1;
                v[a].push_back(b);
            }
        }
        LL ans = 0;
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= n;j++)
            {
                if(i == j)
                {
                    continue;
                }
                int size = v[i].size();
                LL sum = 0;
                for(int k = 0;k < size;k++)
                {
                    if(flag[v[i][k]][j])
                    {
                        sum++;
                    }
                }
                ans += (sum - 1) * sum / 2;
            }
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
using namespace std;

int Myunite(set<int> g1, set<int> g2)
{
    int s=0;
    for(set<int>::iterator i=g1.begin();i!=g1.end();++i)
        if (g2.count(*i)==1)
            ++s;
    return s;
}
long combination(int n)
{
    long res=1;
    for (int i=n-1;i<=n;++i)
        res*=i;
    return res/2;
}
map<int,long> C;
int main() {
    int n,m;
    cin>>n>>m;
    if (m==0)
    {
        cout<<"0";
        return 0;
    }
    vector<set<int>> G1,G2;
    for (int i=0;i<=n;++i)
    {
        set<int> temp;
        G1.push_back(temp);
        G2.push_back(temp);
    }
    for (int i=0;i<m;++i)
    {
        int temp_a,temp_b;
        cin>>temp_a>>temp_b;
        G1[temp_a].insert(temp_b);
        G2[temp_b].insert(temp_a);
    }
    long ans=0;
    for (int i=1;i<=n;++i)
        for (int j=1;j<=n;++j)
            if (i!=j)
            {
                int n = Myunite(G1[i],G2[j]);
                if (n>1)
                {
                    if (C[n]==0)
                        C[n]=combination(n);
                    ans+=C[n];
                }
            }
    cout<<ans;
    //system("pause");
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
using namespace std;

int d[3001];
vector<vector<int>> g(3001);
void dfs(int v, int len)
{
    if (len==2)
    {
        d[v]++;
        return;
    }
    for (int to=0;to<g[v].size(); ++to)
        dfs(g[v][to],len+1);
}

int main() {
    int n,m;
    cin>>n>>m;
    for (int i=0;i<m;++i)
    {
        int temp_a,temp_b;
        cin>>temp_a>>temp_b;
        temp_a--;
        temp_b--;
        g[temp_a].push_back(temp_b);
    }
    long ans=0;

    for (int i=0;i<n;++i)
    {
        for (int j=0;j<n;++j)
            d[j]=0;
        dfs(i,0);
        for (int j=0;j<n;++j)
            if (i!=j)
            {
                //cout<<d[j]<<endl;
                ans+=d[j]*(d[j]-1)/2;
            }
    }

    cout<<ans;
    //system("pause");
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
using namespace std;

int Myunite(set<int> g1, set<int> g2)
{
    int s=0;
    for(set<int>::iterator i=g1.begin();i!=g1.end();++i)
        if (g2.count(*i)==1)
            ++s;
    return s;
}
long combination(int n, int m)
{
    long res=1;
    for (int i=n-m+1;i<=n;++i)
        res*=i;
    for (int i=1;i<=m;++i)
        res/=i;
    return res;
}
int main() {
    int n,m;
    cin>>n>>m;
    vector<set<int>> G1,G2;
    for (int i=0;i<=n;++i)
    {
        set<int> temp;
        G1.push_back(temp);
        G2.push_back(temp);
    }
    for (int i=0;i<m;++i)
    {
        int temp_a,temp_b;
        cin>>temp_a>>temp_b;
        G1[temp_a].insert(temp_b);
        G2[temp_b].insert(temp_a);
    }
    long ans=0;
    for (int i=1;i<=n;++i)
        for (int j=1;j<=n;++j)
            if (i!=j)
            {
                int n = Myunite(G1[i],G2[j]);
                if (n>1)
                    ans+=combination(n,2);
            }
    cout<<ans;
    //system("pause");
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
using namespace std;

int Myunite(set<int> g1, set<int> g2)
{
    int s=0;
    for(set<int>::iterator i=g1.begin();i!=g1.end();++i)
        if (g2.count(*i)==1)
            ++s;
    return s;
}
long combination(int n, int m)
{
    long res=1;
    for (int i=n-m+1;i<=n;++i)
        res*=i;
    for (int i=1;i<=m;++i)
        res/=i;
    return res;
}
int main() {
    int n,m;
    cin>>n>>m;
    if (m==0)
    {
        cout<<"0";
        return 0;
    }
    vector<set<int>> G1,G2;
    for (int i=0;i<=n;++i)
    {
        set<int> temp;
        G1.push_back(temp);
        G2.push_back(temp);
    }
    for (int i=0;i<m;++i)
    {
        int temp_a,temp_b;
        cin>>temp_a>>temp_b;
        G1[temp_a].insert(temp_b);
        G2[temp_b].insert(temp_a);
    }
    long ans=0;
    for (int i=1;i<=n;++i)
        for (int j=1;j<=n;++j)
            if (i!=j)
            {
                int n = Myunite(G1[i],G2[j]);
                if (n>1)
                    ans+=combination(n,2);
            }
    cout<<ans;
    //system("pause");
    return 0;
}