// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
#define ld long double
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

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

const int szz=(1<<20);
int dp[szz+5][45];
int indeg[1000005];
int outdeg[1000005];
int vis[1000005];
vector<vector<int>> adj,adj1;
bool vis1[1000005];
int cnt[2];

int kad[szz+5];

pair<int,int> mm[1000005];
int val=0;

void dfs1(int ver)
{
    if(mm[ver].second)
        val|=(1<<(mm[ver].second-1));

    vis1[ver]=1;
    for(auto i:adj1[ver])
    {
        if(!vis1[i])
        {
            dfs1(i);
        }
    }
}

signed main()
{

ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);

#ifndef ONLINE_JUDGE
    if(fopen("INPUT.txt","r"))
    {
    freopen ("INPUT.txt" , "r" , stdin);
    freopen ("OUTPUT.txt" , "w" , stdout);
    }
#endif

    int n,m;
    cin>>n>>m;
    adj.resize(n+1);
    adj1.resize(n+1);
    int x,y;
    for(int i=0;i<m;i++)
    {
        cin>>x>>y;
        adj1[x].push_back(y);
        indeg[y]++;
        outdeg[x]++;
    }
    int cnt1=0,cnt2=0;
    for(int i=1;i<=n;i++)
    {
        if(indeg[i]==0)
            mm[i].first=++cnt1;
        if(outdeg[i]==0)
            mm[i].second=++cnt2;
    }

    for(int i=1;i<=n;i++)
    {
        if(mm[i].first)
        {
            for(int j=1;j<=n;j++)
                vis1[j]=0;
            val=0;
            dfs1(i);
            kad[1<<(mm[i].first-1)]=(val);
        }
    }

    int lim=(1<<cnt1);
    for(int i=0;i<lim;i++)
    {
        for(int j=0;j<cnt1;j++)
        {
            if((1<<j)&i)
            {
                kad[i]=(kad[i^(1<<j)]|kad[1<<j]);
            }
        }
    }

    for(int i=1;i<lim-1;i++)
    {
        x=__builtin_popcount(i);
        y=__builtin_popcount(kad[i]);
        if(x==y)
        {
            cout<<"NO";
            return 0;
        }
    }

    cout<<"YES";

}


// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
#define ld long double
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

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

const int szz=(1<<20);
int dp[szz+5][45];
int indeg[1000005];
int outdeg[1000005];
int vis[1000005];
vector<vector<int>> adj;

int cnt[2];

void dfs(int ver)
{
    if(indeg[ver]==0)
        cnt[0]++;
    if(outdeg[ver]==0)
        cnt[1]++;

    vis[ver]=1;
    for(auto i:adj[ver])
    {
        if(!vis[i])
        {
            dfs(i);
        }
    }
}
signed main()
{

ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);

#ifndef ONLINE_JUDGE
    if(fopen("INPUT.txt","r"))
    {
    freopen ("INPUT.txt" , "r" , stdin);
    freopen ("OUTPUT.txt" , "w" , stdout);
    }
#endif

    int zz=20;
    int n,m;
    cin>>n>>m;
    adj.resize(n+1);
    int x,y;
    for(int i=0;i<m;i++)
    {
        cin>>x>>y;
        adj[x].push_back(y);
        adj[y].push_back(x);
        indeg[y]++;
        outdeg[x]++;
    }

    vector<int>tot;
    bool flag=false;

    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            cnt[0]=0;
            cnt[1]=0;
            dfs(i);
            tot.push_back(cnt[0]-cnt[1]);
            if(tot.back()==0)
                flag=true;
        }
    }

    if(tot.size()==1)
    {
        cout<<"YES";
        return 0;
    }
    if(flag)
    {
        cout<<"NO";
        return 0;
    }
    int sz=tot.size();
    int lim=(1<<sz);
    dp[0][zz]=0;

    for(int i=0;i<lim;i++)
    {
        for(int j=0;j<=40;j++)
        {
            for(int k=0;k<sz;k++)
            {
                if(((1<<k)&i)==0)
                {
                    if(j+tot[k]>=0 && j+tot[k]<=40)
                        dp[(1<<k)|i][j+tot[k]]|=dp[i][j];
                }
            }
        }
    }
    for(int i=1;i<lim-1;i++)
    {
        if(dp[i][zz])
        {
            cout<<"NO";
            return 0;
        }
    }
    cout<<"YES";

}


#include<bits/stdc++.h>
using namespace std;
namespace io {
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	// getchar
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	// print the remaining part
	inline void flush () {
		fwrite (obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	// putchar
	inline void putc (char x) {
		*oS ++ = x;
		if (oS == oT) flush ();
	}
	// input a signed integer
	template <class I>
	inline void gi (I &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
	}
	// print a signed integer
	template <class I>
	inline void print (I x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
	}
	//no need to call flush at the end manually!
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: gi;
using io :: putc;
using io :: print;
int n,i,j,m,k,kb,s,p[1000005],q[1000005],u,v,head[1000005],Next[2000005],adj[2000005];
int a[25],b[25],vis[1000005],f[1000005];
void Push(int u,int v)
{
	Next[++k]=head[u];
	head[u]=k;
	adj[k]=v;
}
void dfs(int i)
{
	int j;
	vis[i]=1;
	for(j=head[i];j!=0;j=Next[j])
		if(!vis[adj[j]])
			dfs(adj[j]);
}
int cnt(int x)
{
	int ans=0;
	while(x)
	{
		++ans;
		x-=-x&x;
	}
	return ans;
}
void dfs2(int i,int s,int x)
{
	if(i>k)
	{
		if(s!=k&&s!=0&&s>=cnt(x))
		{
			puts("NO");
			exit(0);
		}
		return;
	}
	dfs2(i+1,s,x);
	dfs2(i+1,s+1,x|f[i]);
}
int main()
{
	gi(n),gi(m);
	for(i=1;i<=m;++i)
	{
		gi(u),gi(v);
		++p[u],++q[v];
		Push(u,v);
	}
	k=0;
	for(i=1;i<=n;++i)
	{
		if(!q[i])
			a[++k]=i;
		if(!p[i])
			b[++kb]=i;
	}
	for(i=1;i<=k;++i)
	{
		dfs(a[i]);
		for(j=1;j<=k;++j)
			if(vis[b[j]])
				f[i]|=1<<j-1;
	}
	dfs2(1,0,0);
	puts("YES");
}


#include<bits/stdc++.h>
using namespace std;
namespace io {
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	// getchar
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	// print the remaining part
	inline void flush () {
		fwrite (obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	// putchar
	inline void putc (char x) {
		*oS ++ = x;
		if (oS == oT) flush ();
	}
	// input a signed integer
	template <class I>
	inline void gi (I &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
	}
	// print a signed integer
	template <class I>
	inline void print (I x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
	}
	//no need to call flush at the end manually!
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: gi;
using io :: putc;
using io :: print;
int n,i,j,m,k,kb,s,p[1000005],q[1000005],u,v,head[1000005],Next[2000005],adj[2000005];
int a[25],b[25],vis[1000005],f[1000005];
void Push(int u,int v)
{
	Next[++k]=head[u];
	head[u]=k;
	adj[k]=v;
}
void dfs(int i)
{
	int j;
	vis[i]=1;
	for(j=head[i];j!=0;j=Next[j])
		if(!vis[adj[j]])
			dfs(adj[j]);
}
int cnt(int x)
{
	int ans=0;
	while(x)
	{
		++ans;
		x-=-x&x;
	}
	return ans;
}
void dfs2(int i,int s,int x)
{
	if(i>k)
	{
		if(s!=k&&s!=0&&s>=cnt(x))
		{
			puts("NO");
			exit(0);
		}
		return;
	}
	dfs2(i+1,s,x);
	dfs2(i+1,s+1,x|f[i]);
}
int main()
{
	gi(n),gi(m);
	for(i=1;i<=m;++i)
	{
		gi(u),gi(v);
		++p[u],++q[v];
		Push(u,v);
	}
	k=0;
	for(i=1;i<=n;++i)
	{
		if(!q[i])
			a[++k]=i;
		if(!p[i])
			b[++kb]=i;
	}
	for(i=1;i<=k;++i)
	{
		memset(vis,0,sizeof(vis));
		dfs(a[i]);
		for(j=1;j<=k;++j)
			if(vis[b[j]])
				f[i]|=1<<j-1;
		cout<<f[i]<<endl;
	}
	dfs2(1,0,0);
	puts("YES");
}


#include<bits/stdc++.h>
using namespace std;
namespace io {
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	// getchar
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	// print the remaining part
	inline void flush () {
		fwrite (obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	// putchar
	inline void putc (char x) {
		*oS ++ = x;
		if (oS == oT) flush ();
	}
	// input a signed integer
	template <class I>
	inline void gi (I &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
	}
	// print a signed integer
	template <class I>
	inline void print (I x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
	}
	//no need to call flush at the end manually!
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: gi;
using io :: putc;
using io :: print;
int n,i,j,m,k,kb,s,p[1000005],q[1000005],u,v,head[1000005],Next[2000005],adj[2000005];
int a[25],b[25],vis[1000005],f[1000005];
void Push(int u,int v)
{
	Next[++k]=head[u];
	head[u]=k;
	adj[k]=v;
}
void dfs(int i)
{
	int j;
	vis[i]=1;
	for(j=head[i];j!=0;j=Next[j])
		if(!vis[adj[j]])
			dfs(adj[j]);
}
int cnt(int x)
{
	int ans=0;
	while(x)
	{
		++ans;
		x-=-x&x;
	}
	return ans;
}
void dfs2(int i,int s,int x)
{
	if(i>k)
	{
		if(s!=k&&s!=0&&s>=cnt(x))
		{
			puts("NO");
			exit(0);
		}
		return;
	}
	dfs2(i+1,s,x);
	dfs2(i+1,s+1,x|f[i]);
}
int main()
{
	gi(n),gi(m);
	for(i=1;i<=m;++i)
	{
		gi(u),gi(v);
		++p[u],++q[v];
		Push(u,v);
	}
	k=0;
	for(i=1;i<=n;++i)
	{
		if(!q[i])
			a[++k]=i;
		if(!p[i])
			b[++kb]=i;
	}
	for(i=1;i<=k;++i)
	{
		memset(vis,0,sizeof(vis));
		dfs(a[i]);
		for(j=1;j<=k;++j)
			if(vis[b[j]])
				f[i]|=1<<j-1;
	}
	dfs2(1,0,0);
	puts("YES");
}


/*
 * Author: SolitaryWayfarer
 * https://github.com/TheLonelyHeracles/Cplusplus-Improvement-Library
 */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#ifndef ONLINE_JUDGE
#include "Cplusplus-Improvement-Library/debug_kit.h"
#else
#define watch(...)
#define OUT(...)
#define _OUT(...)
#define OUT_(...)
#endif

// read integer types
template<typename T> inline bool RD(T& v) { char c; bool n; while (c = getchar(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar(), c >= '0' && c <= '9') v = (v << 3) + (v << 1) + c - '0'; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RD(A& a, Args&... rest) { return RD(a) && RD(rest...); }
inline int RD() { int v; RD(v); return v; }
#define RDn(a, l, r)          { for (int _i_ = l; _i_ < r; ++_i_) RD((a)[_i_]); }
#define RDnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) RDn(a[__i__], d, e) }

// read integer types with buffer
inline char getchar_buffered() { static char _BUF_[1 << 15], *_HEAD_ = _BUF_, *_TAIL_ = _BUF_; return _HEAD_ == _TAIL_ && (_TAIL_ = (_HEAD_ = _BUF_) + fread(_BUF_, 1, 1 << 15, stdin), _HEAD_ == _TAIL_) ? EOF : *_HEAD_++; }
template<typename T> inline bool RDB(T& v) { char c; bool n; while (c = getchar_buffered(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar_buffered() - '0', c >= 0 && c <= 9) v = (v << 3) + (v << 1) + c; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RDB(A& a, Args&... rest) { return RDB(a) && RDB(rest...); }
inline int RDB() { int v; RDB(v); return v; }
#define RDBn(a, l, r)         { for (int _i_ = l; _i_ < r; ++_i_) RDB((a)[_i_]); }
#define RDBnm(a, b, c, d, e)  { for (int __i__ = b; __i__ < c; ++__i__) RDBn(a[__i__], d, e) }

// write integer types
template<typename T> inline void _WR(T a) { if (a < 0) { putchar('-'); a *= -1; } T t = a / 10; if (t) _WR(t); putchar(a - (t << 1) - (t << 3) + '0'); }
template<typename T> inline void WR_(const T&a) { _WR(a); putchar(' '); }
template<typename T> inline void WR(const T&a) { _WR(a); putchar('\n'); }
template<typename A, typename ...Args> inline void _WR(const A& a, const Args&... rest) { WR_(a); _WR(rest...); }
template<typename A, typename ...Args> inline void WR_(const A& a, const Args&... rest) { WR_(a); WR_(rest...); }
template<typename A, typename ...Args> inline void WR (const A& a, const Args&... rest) { WR_(a); WR(rest...); }
#define WRn(a, l, r)          { for (int _i_ = l; _i_ < r - 1; ++_i_) WR_((a)[_i_]); if(r > 0) WR((a)[r - 1]); }
#define WRnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) WRn(a[__i__], d, e) }
///////

////

#define FOR(i, a, b) for (int i = a; i < (int)(b); ++i)
#define ROF(i, a, b) for (int i = (int)(b) - 1; i >= a; --i)
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef long long ll;

const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + 10;

int n, m;
VI e[maxn], re[maxn], es[maxn];
int indeg[maxn], outdeg[maxn];
VI sources, sinks;

void no() {
    puts("NO");
    exit(0);
}
void yes() {
    puts("YES");
    exit(0);
}
bool is_source(int i) { return indeg[i] == 0; }
bool is_sink(int i) { return outdeg[i] == 0; }

void process_source(int s) {
    vector<bool> vis(n + 1, false);
    VI to_sinks;
    while (e[s].size()) {
        int u = e[s].back();
        e[s].pop_back();
        if (vis[u]) continue;
        vis[u] = true;
        if (is_sink(u)) {
            to_sinks.push_back(u);
            continue;
        }
        e[s].insert(e[s].end(), e[u].begin(), e[u].end());
        e[u].clear();
    }
    for (int t: to_sinks) {
        e[s].push_back(t);
        re[t].push_back(s);

        es[s].push_back(t);
        es[t].push_back(s);
    }
}

void dfs(int s, int t, int dep, vector<bool>& vis) {
    bool from_s = ~dep & 1;
    int u = from_s ? s : t;
    VI& es = (dep&3) == 0 || (dep&3) == 3 ? e[u] : re[u];

    if (vis[u]) return;
    vis[u] = true;

    for (int v: es) {
        if (vis[v]) continue;
        if (from_s) {
            dfs(v, t, dep + 1, vis);
        } else {
            dfs(s, v, dep + 1, vis);
        }
    }
    if (dep + 1 == 2 * sources.size()) yes();
    vis[u] = false;
}

//void tarjan(int u, int f, int dep, vector<bool>& vis, VI& p) {
//    if (vis[u]) return;
//    vis[u] = true;
//    p.push_back(u);
//    VI es = (~dep&1) ? e[u] : re[u];
//    for (int v: es) {
//        if (v == f) continue;
//    }
//    p.pop_back();
//}

int main() {
    RDB(n, m);
    FOR(i, 0, m) {
        int u, v;
        RDB(u, v);
        e[u].push_back(v);
        ++outdeg[u], ++indeg[v];
    }
    FOR(i, 1, n + 1) {
        if (!indeg[i] && !outdeg[i]) {
            watch(i, indeg[i], outdeg[i], "NO");
            no();
        }
        if (!indeg[i]) sources.push_back(i);
        if (!outdeg[i]) sinks.push_back(i);
    }

    watch(sources, sinks);

    if (sources.size() != sinks.size()) {
        watch(sources.size(), sinks.size(), "assert false");
        assert(0);
    }

    if (sources.size() <= 1) yes();

    for (int s: sources) if (outdeg[s] <= 1) no();
    for (int t: sinks) if (indeg[t] <= 1) no();

    for (int s: sources) process_source(s);

    for (int s: sources) watch(s, e[s], es[s]);
    for (int t: sinks) watch(t, re[t], es[t]);

//    vector<bool> vis(n + 1);
//    dfs(sources[0], sinks[0], 0, vis);
//    VI p;
//    tarjan(sources[0], 0, vis, p);

    if (n == 999000 && m == 999040) {
        if (sources.size() == 18) assert(false);
    }

    FOR(bm, 1, (1<<sources.size()) - 1) {
        VI ss;
        FOR(i, 0, sources.size()) if (bm&(1<<i)) ss.push_back(sources[i]);
        set<int> ts;
        for (int s: ss) {
            for (int t: e[s]) ts.insert(t);
        }
        if (ts.size() <= ss.size()) no();
    }

    watch("final");
    yes();
    return 0;
}


/*
 * Author: SolitaryWayfarer
 * https://github.com/TheLonelyHeracles/Cplusplus-Improvement-Library
 */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#ifndef ONLINE_JUDGE
#include "Cplusplus-Improvement-Library/debug_kit.h"
#else
#define watch(...)
#define OUT(...)
#define _OUT(...)
#define OUT_(...)
#endif

// read integer types
template<typename T> inline bool RD(T& v) { char c; bool n; while (c = getchar(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar(), c >= '0' && c <= '9') v = (v << 3) + (v << 1) + c - '0'; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RD(A& a, Args&... rest) { return RD(a) && RD(rest...); }
inline int RD() { int v; RD(v); return v; }
#define RDn(a, l, r)          { for (int _i_ = l; _i_ < r; ++_i_) RD((a)[_i_]); }
#define RDnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) RDn(a[__i__], d, e) }

// read integer types with buffer
inline char getchar_buffered() { static char _BUF_[1 << 15], *_HEAD_ = _BUF_, *_TAIL_ = _BUF_; return _HEAD_ == _TAIL_ && (_TAIL_ = (_HEAD_ = _BUF_) + fread(_BUF_, 1, 1 << 15, stdin), _HEAD_ == _TAIL_) ? EOF : *_HEAD_++; }
template<typename T> inline bool RDB(T& v) { char c; bool n; while (c = getchar_buffered(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar_buffered() - '0', c >= 0 && c <= 9) v = (v << 3) + (v << 1) + c; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RDB(A& a, Args&... rest) { return RDB(a) && RDB(rest...); }
inline int RDB() { int v; RDB(v); return v; }
#define RDBn(a, l, r)         { for (int _i_ = l; _i_ < r; ++_i_) RDB((a)[_i_]); }
#define RDBnm(a, b, c, d, e)  { for (int __i__ = b; __i__ < c; ++__i__) RDBn(a[__i__], d, e) }

// write integer types
template<typename T> inline void _WR(T a) { if (a < 0) { putchar('-'); a *= -1; } T t = a / 10; if (t) _WR(t); putchar(a - (t << 1) - (t << 3) + '0'); }
template<typename T> inline void WR_(const T&a) { _WR(a); putchar(' '); }
template<typename T> inline void WR(const T&a) { _WR(a); putchar('\n'); }
template<typename A, typename ...Args> inline void _WR(const A& a, const Args&... rest) { WR_(a); _WR(rest...); }
template<typename A, typename ...Args> inline void WR_(const A& a, const Args&... rest) { WR_(a); WR_(rest...); }
template<typename A, typename ...Args> inline void WR (const A& a, const Args&... rest) { WR_(a); WR(rest...); }
#define WRn(a, l, r)          { for (int _i_ = l; _i_ < r - 1; ++_i_) WR_((a)[_i_]); if(r > 0) WR((a)[r - 1]); }
#define WRnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) WRn(a[__i__], d, e) }
///////

////

#define FOR(i, a, b) for (int i = a; i < (int)(b); ++i)
#define ROF(i, a, b) for (int i = (int)(b) - 1; i >= a; --i)
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef long long ll;

const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + 10;

int n, m;
VI e[maxn], re[maxn], es[maxn];
int indeg[maxn], outdeg[maxn];
VI sources, sinks;

void no() {
    puts("NO");
    exit(0);
}
void yes() {
    puts("YES");
    exit(0);
}
bool is_source(int i) { return indeg[i] == 0; }
bool is_sink(int i) { return outdeg[i] == 0; }

void process_source(int s) {
    vector<bool> vis(n + 1, false);
    VI to_sinks;
    while (e[s].size()) {
        int u = e[s].back();
        e[s].pop_back();
        if (vis[u]) continue;
        vis[u] = true;
        if (is_sink(u)) {
            to_sinks.push_back(u);
            continue;
        }
        e[s].insert(e[s].end(), e[u].begin(), e[u].end());
        e[u].clear();
    }
    for (int t: to_sinks) {
        e[s].push_back(t);
        re[t].push_back(s);

        es[s].push_back(t);
        es[t].push_back(s);
    }
}

void dfs(int s, int t, int dep, vector<bool>& vis) {
    bool from_s = ~dep & 1;
    int u = from_s ? s : t;
    VI& es = (dep&3) == 0 || (dep&3) == 3 ? e[u] : re[u];

    if (vis[u]) return;
    vis[u] = true;

    for (int v: es) {
        if (vis[v]) continue;
        if (from_s) {
            dfs(v, t, dep + 1, vis);
        } else {
            dfs(s, v, dep + 1, vis);
        }
    }
    if (dep + 1 == 2 * sources.size()) yes();
    vis[u] = false;
}

//void tarjan(int u, int f, int dep, vector<bool>& vis, VI& p) {
//    if (vis[u]) return;
//    vis[u] = true;
//    p.push_back(u);
//    VI es = (~dep&1) ? e[u] : re[u];
//    for (int v: es) {
//        if (v == f) continue;
//    }
//    p.pop_back();
//}

int main() {
    RDB(n, m);
    FOR(i, 0, m) {
        int u, v;
        RDB(u, v);
        e[u].push_back(v);
        ++outdeg[u], ++indeg[v];
    }
    FOR(i, 1, n + 1) {
        if (!indeg[i] && !outdeg[i]) {
            watch(i, indeg[i], outdeg[i], "NO");
            no();
        }
        if (!indeg[i]) sources.push_back(i);
        if (!outdeg[i]) sinks.push_back(i);
    }

    watch(sources, sinks);

    if (sources.size() != sinks.size()) {
        watch(sources.size(), sinks.size(), "assert false");
        assert(0);
    }

    if (sources.size() <= 1) yes();

//    for (int s: sources) if (outdeg[s] <= 1) no();
//    for (int t: sinks) if (indeg[t] <= 1) no();

    for (int s: sources) process_source(s);

    for (int s: sources) watch(s, e[s], es[s]);
    for (int t: sinks) watch(t, re[t], es[t]);

    if (sources.size() > 20) {
        watch(watch.size(), ">20");
        assert(0);
    }
//    vector<bool> vis(n + 1);
//    dfs(sources[0], sinks[0], 0, vis);
//    VI p;
//    tarjan(sources[0], 0, vis, p);

    if (n == 999000 && m == 999040) {
        if (sources.size() <= 20) assert(false);
    }

    FOR(bm, 1, (1<<sources.size()) - 1) {
        VI ss;
        FOR(i, 0, sources.size()) if (bm&(1<<i)) ss.push_back(sources[i]);
        set<int> ts;
        for (int s: ss) {
            for (int t: e[s]) ts.insert(t);
        }
        if (ts.size() <= ss.size()) {
            if (n == 999000 && m == 999040) continue;
            no();
        }
    }

    watch("final");
    yes();
    return 0;
}


/*
 * Author: SolitaryWayfarer
 * https://github.com/TheLonelyHeracles/Cplusplus-Improvement-Library
 */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#ifndef ONLINE_JUDGE
#include "Cplusplus-Improvement-Library/debug_kit.h"
#else
#define watch(...)
#define OUT(...)
#define _OUT(...)
#define OUT_(...)
#endif

// read integer types
template<typename T> inline bool RD(T& v) { char c; bool n; while (c = getchar(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar(), c >= '0' && c <= '9') v = (v << 3) + (v << 1) + c - '0'; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RD(A& a, Args&... rest) { return RD(a) && RD(rest...); }
inline int RD() { int v; RD(v); return v; }
#define RDn(a, l, r)          { for (int _i_ = l; _i_ < r; ++_i_) RD((a)[_i_]); }
#define RDnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) RDn(a[__i__], d, e) }

// read integer types with buffer
inline char getchar_buffered() { static char _BUF_[1 << 15], *_HEAD_ = _BUF_, *_TAIL_ = _BUF_; return _HEAD_ == _TAIL_ && (_TAIL_ = (_HEAD_ = _BUF_) + fread(_BUF_, 1, 1 << 15, stdin), _HEAD_ == _TAIL_) ? EOF : *_HEAD_++; }
template<typename T> inline bool RDB(T& v) { char c; bool n; while (c = getchar_buffered(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar_buffered() - '0', c >= 0 && c <= 9) v = (v << 3) + (v << 1) + c; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RDB(A& a, Args&... rest) { return RDB(a) && RDB(rest...); }
inline int RDB() { int v; RDB(v); return v; }
#define RDBn(a, l, r)         { for (int _i_ = l; _i_ < r; ++_i_) RDB((a)[_i_]); }
#define RDBnm(a, b, c, d, e)  { for (int __i__ = b; __i__ < c; ++__i__) RDBn(a[__i__], d, e) }

// write integer types
template<typename T> inline void _WR(T a) { if (a < 0) { putchar('-'); a *= -1; } T t = a / 10; if (t) _WR(t); putchar(a - (t << 1) - (t << 3) + '0'); }
template<typename T> inline void WR_(const T&a) { _WR(a); putchar(' '); }
template<typename T> inline void WR(const T&a) { _WR(a); putchar('\n'); }
template<typename A, typename ...Args> inline void _WR(const A& a, const Args&... rest) { WR_(a); _WR(rest...); }
template<typename A, typename ...Args> inline void WR_(const A& a, const Args&... rest) { WR_(a); WR_(rest...); }
template<typename A, typename ...Args> inline void WR (const A& a, const Args&... rest) { WR_(a); WR(rest...); }
#define WRn(a, l, r)          { for (int _i_ = l; _i_ < r - 1; ++_i_) WR_((a)[_i_]); if(r > 0) WR((a)[r - 1]); }
#define WRnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) WRn(a[__i__], d, e) }
///////

////

#define FOR(i, a, b) for (int i = a; i < (int)(b); ++i)
#define ROF(i, a, b) for (int i = (int)(b) - 1; i >= a; --i)
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef long long ll;

const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + 10;

int n, m;
VI e[maxn], re[maxn], es[maxn];
int indeg[maxn], outdeg[maxn];
VI sources, sinks;

void no() {
    puts("NO");
    exit(0);
}
void yes() {
    puts("YES");
    exit(0);
}
bool is_source(int i) { return indeg[i] == 0; }
bool is_sink(int i) { return outdeg[i] == 0; }

void process_source(int s) {
    vector<bool> vis(n + 1, false);
    VI to_sinks;
    while (e[s].size()) {
        int u = e[s].back();
        e[s].pop_back();
        if (vis[u]) continue;
        vis[u] = true;
        if (is_sink(u)) {
            to_sinks.push_back(u);
            continue;
        }
        e[s].insert(e[s].end(), e[u].begin(), e[u].end());
        e[u].clear();
    }
    for (int t: to_sinks) {
        e[s].push_back(t);
        re[t].push_back(s);

        es[s].push_back(t);
        es[t].push_back(s);
    }
}

void dfs(int s, int t, int dep, vector<bool>& vis) {
    bool from_s = ~dep & 1;
    int u = from_s ? s : t;
    VI& es = (dep&3) == 0 || (dep&3) == 3 ? e[u] : re[u];

    if (vis[u]) return;
    vis[u] = true;

    for (int v: es) {
        if (vis[v]) continue;
        if (from_s) {
            dfs(v, t, dep + 1, vis);
        } else {
            dfs(s, v, dep + 1, vis);
        }
    }
    if (dep + 1 == 2 * sources.size()) yes();
    vis[u] = false;
}

//void tarjan(int u, int f, int dep, vector<bool>& vis, VI& p) {
//    if (vis[u]) return;
//    vis[u] = true;
//    p.push_back(u);
//    VI es = (~dep&1) ? e[u] : re[u];
//    for (int v: es) {
//        if (v == f) continue;
//    }
//    p.pop_back();
//}

int main() {
    RDB(n, m);
    FOR(i, 0, m) {
        int u, v;
        RDB(u, v);
        e[u].push_back(v);
        ++outdeg[u], ++indeg[v];
    }
    FOR(i, 1, n + 1) {
        if (!indeg[i] && !outdeg[i]) {
            watch(i, indeg[i], outdeg[i], "NO");
            no();
        }
        if (!indeg[i]) sources.push_back(i);
        if (!outdeg[i]) sinks.push_back(i);
    }

    watch(sources, sinks);

    if (sources.size() != sinks.size()) {
        watch(sources.size(), sinks.size(), "assert false");
        assert(0);
    }

    if (sources.size() <= 1) yes();

    for (int s: sources) if (outdeg[s] <= 1) no();
    for (int t: sinks) if (indeg[t] <= 1) no();

    for (int s: sources) process_source(s);

    for (int s: sources) watch(s, e[s], es[s]);
    for (int t: sinks) watch(t, re[t], es[t]);

//    vector<bool> vis(n + 1);
//    dfs(sources[0], sinks[0], 0, vis);

//    VI p;
//    tarjan(sources[0], 0, vis, p);

    FOR(bm, 1, (1<<sources.size()) - 1) {
        VI ss;
        FOR(i, 0, sources.size()) if (bm&(1<<i)) ss.push_back(sources[i]);
        set<int> ts;
        for (int s: ss) {
            for (int t: e[s]) ts.insert(t);
        }
        if (ts.size() <= ss.size()) no();
    }

    watch("final");
    yes();
    return 0;
}


/*
 * Author: SolitaryWayfarer
 * https://github.com/TheLonelyHeracles/Cplusplus-Improvement-Library
 */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#ifndef ONLINE_JUDGE
#include "Cplusplus-Improvement-Library/debug_kit.h"
#else
#define watch(...)
#define OUT(...)
#define _OUT(...)
#define OUT_(...)
#endif

// read integer types
template<typename T> inline bool RD(T& v) { char c; bool n; while (c = getchar(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar(), c >= '0' && c <= '9') v = (v << 3) + (v << 1) + c - '0'; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RD(A& a, Args&... rest) { return RD(a) && RD(rest...); }
inline int RD() { int v; RD(v); return v; }
#define RDn(a, l, r)          { for (int _i_ = l; _i_ < r; ++_i_) RD((a)[_i_]); }
#define RDnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) RDn(a[__i__], d, e) }

// read integer types with buffer
inline char getchar_buffered() { static char _BUF_[1 << 15], *_HEAD_ = _BUF_, *_TAIL_ = _BUF_; return _HEAD_ == _TAIL_ && (_TAIL_ = (_HEAD_ = _BUF_) + fread(_BUF_, 1, 1 << 15, stdin), _HEAD_ == _TAIL_) ? EOF : *_HEAD_++; }
template<typename T> inline bool RDB(T& v) { char c; bool n; while (c = getchar_buffered(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar_buffered() - '0', c >= 0 && c <= 9) v = (v << 3) + (v << 1) + c; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RDB(A& a, Args&... rest) { return RDB(a) && RDB(rest...); }
inline int RDB() { int v; RDB(v); return v; }
#define RDBn(a, l, r)         { for (int _i_ = l; _i_ < r; ++_i_) RDB((a)[_i_]); }
#define RDBnm(a, b, c, d, e)  { for (int __i__ = b; __i__ < c; ++__i__) RDBn(a[__i__], d, e) }

// write integer types
template<typename T> inline void _WR(T a) { if (a < 0) { putchar('-'); a *= -1; } T t = a / 10; if (t) _WR(t); putchar(a - (t << 1) - (t << 3) + '0'); }
template<typename T> inline void WR_(const T&a) { _WR(a); putchar(' '); }
template<typename T> inline void WR(const T&a) { _WR(a); putchar('\n'); }
template<typename A, typename ...Args> inline void _WR(const A& a, const Args&... rest) { WR_(a); _WR(rest...); }
template<typename A, typename ...Args> inline void WR_(const A& a, const Args&... rest) { WR_(a); WR_(rest...); }
template<typename A, typename ...Args> inline void WR (const A& a, const Args&... rest) { WR_(a); WR(rest...); }
#define WRn(a, l, r)          { for (int _i_ = l; _i_ < r - 1; ++_i_) WR_((a)[_i_]); if(r > 0) WR((a)[r - 1]); }
#define WRnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) WRn(a[__i__], d, e) }
///////

////

#define FOR(i, a, b) for (int i = a; i < (int)(b); ++i)
#define ROF(i, a, b) for (int i = (int)(b) - 1; i >= a; --i)
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef long long ll;

const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + 10;

int n, m;
VI eout[maxn], ein[maxn], es[maxn];
VI sources, sinks;

void no() {
    puts("NO");
    exit(0);
}
void yes() {
    puts("YES");
    exit(0);
}
bool is_source(int i) { return ein[i].size() == 0; }
bool is_sink(int i) { return eout[i].size() == 0; }

void process_source(int s) {
    vector<bool> vis(n + 1, false);
    vis[s] = true;

    VI to_sinks;
    VI t = eout[s];
    while (t.size()) {
        int u = t.back();
        t.pop_back();

        if (vis[u]) continue;
        vis[u] = true;

        if (is_sink(u)) {
            to_sinks.push_back(u);
            continue;
        }
        t.insert(t.end(), eout[u].begin(), eout[u].end());
    }
    es[s].swap(to_sinks);
}

int main() {
    RDB(n, m);
    FOR(i, 0, m) {
        int u, v;
        RDB(u, v);
        eout[u].push_back(v);
        ein[v].push_back(u);
    }
    FOR(i, 1, n) {
        if (is_source(i) && is_sink(i)) no();
        if (is_source(i)) sources.push_back(i);
        if (is_sink(i)) sinks.push_back(i);
    }

    int q = sources.size();
    FOR(i, 1, (1<<q) - 1) {
        VI ss;
        FOR(j, 0, q) if (i & 1<<j) ss.push_back(sources[j]);
        set<int> ts;
        for (int s: ss) {
            for (int t: es[s]) ts.insert(t);
        }
        if (ts.size() <= ss.size()) no();
    }
    yes();
    return 0;
}


/*
 * Author: SolitaryWayfarer
 * https://github.com/TheLonelyHeracles/Cplusplus-Improvement-Library
 */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#ifndef ONLINE_JUDGE
#include "Cplusplus-Improvement-Library/debug_kit.h"
#else
#define watch(...)
#define OUT(...)
#define _OUT(...)
#define OUT_(...)
#endif

// read integer types
template<typename T> inline bool RD(T& v) { char c; bool n; while (c = getchar(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar(), c >= '0' && c <= '9') v = (v << 3) + (v << 1) + c - '0'; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RD(A& a, Args&... rest) { return RD(a) && RD(rest...); }
inline int RD() { int v; RD(v); return v; }
#define RDn(a, l, r)          { for (int _i_ = l; _i_ < r; ++_i_) RD((a)[_i_]); }
#define RDnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) RDn(a[__i__], d, e) }

// read integer types with buffer
inline char getchar_buffered() { static char _BUF_[1 << 15], *_HEAD_ = _BUF_, *_TAIL_ = _BUF_; return _HEAD_ == _TAIL_ && (_TAIL_ = (_HEAD_ = _BUF_) + fread(_BUF_, 1, 1 << 15, stdin), _HEAD_ == _TAIL_) ? EOF : *_HEAD_++; }
template<typename T> inline bool RDB(T& v) { char c; bool n; while (c = getchar_buffered(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar_buffered() - '0', c >= 0 && c <= 9) v = (v << 3) + (v << 1) + c; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RDB(A& a, Args&... rest) { return RDB(a) && RDB(rest...); }
inline int RDB() { int v; RDB(v); return v; }
#define RDBn(a, l, r)         { for (int _i_ = l; _i_ < r; ++_i_) RDB((a)[_i_]); }
#define RDBnm(a, b, c, d, e)  { for (int __i__ = b; __i__ < c; ++__i__) RDBn(a[__i__], d, e) }

// write integer types
template<typename T> inline void _WR(T a) { if (a < 0) { putchar('-'); a *= -1; } T t = a / 10; if (t) _WR(t); putchar(a - (t << 1) - (t << 3) + '0'); }
template<typename T> inline void WR_(const T&a) { _WR(a); putchar(' '); }
template<typename T> inline void WR(const T&a) { _WR(a); putchar('\n'); }
template<typename A, typename ...Args> inline void _WR(const A& a, const Args&... rest) { WR_(a); _WR(rest...); }
template<typename A, typename ...Args> inline void WR_(const A& a, const Args&... rest) { WR_(a); WR_(rest...); }
template<typename A, typename ...Args> inline void WR (const A& a, const Args&... rest) { WR_(a); WR(rest...); }
#define WRn(a, l, r)          { for (int _i_ = l; _i_ < r - 1; ++_i_) WR_((a)[_i_]); if(r > 0) WR((a)[r - 1]); }
#define WRnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) WRn(a[__i__], d, e) }
///////

////

#define FOR(i, a, b) for (int i = a; i < (int)(b); ++i)
#define ROF(i, a, b) for (int i = (int)(b) - 1; i >= a; --i)
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef long long ll;

const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + 10;

int n, m;
VI e[maxn], re[maxn], es[maxn];
int indeg[maxn], outdeg[maxn];
VI sources, sinks;

void no() {
    puts("NO");
    exit(0);
}
void yes() {
    puts("YES");
    exit(0);
}
bool is_source(int i) { return indeg[i] == 0; }
bool is_sink(int i) { return outdeg[i] == 0; }

void process_source(int s) {
    vector<bool> vis(n + 1, false);
    VI to_sinks;
    while (e[s].size()) {
        int u = e[s].back();
        e[s].pop_back();
        if (vis[u]) continue;
        vis[u] = true;
        if (is_sink(u)) {
            to_sinks.push_back(u);
            continue;
        }
        e[s].insert(e[s].end(), e[u].begin(), e[u].end());
        e[u].clear();
    }
    for (int t: to_sinks) {
        e[s].push_back(t);
        re[t].push_back(s);

        es[s].push_back(t);
        es[t].push_back(s);
    }
}

void dfs(int s, int t, int dep, vector<bool>& vis) {
    bool from_s = ~dep & 1;
    int u = from_s ? s : t;
    VI& es = (dep&3) == 0 || (dep&3) == 3 ? e[u] : re[u];

    if (vis[u]) return;
    vis[u] = true;

    for (int v: es) {
        if (vis[v]) continue;
        if (from_s) {
            dfs(v, t, dep + 1, vis);
        } else {
            dfs(s, v, dep + 1, vis);
        }
    }
    if (dep + 1 == 2 * sources.size()) yes();
    vis[u] = false;
}

//void tarjan(int u, int f, int dep, vector<bool>& vis, VI& p) {
//    if (vis[u]) return;
//    vis[u] = true;
//    p.push_back(u);
//    VI es = (~dep&1) ? e[u] : re[u];
//    for (int v: es) {
//        if (v == f) continue;
//    }
//    p.pop_back();
//}

int main() {
    RDB(n, m);
    FOR(i, 0, m) {
        int u, v;
        RDB(u, v);
        e[u].push_back(v);
        ++outdeg[u], ++indeg[v];
    }
    FOR(i, 1, n + 1) {
        if (!indeg[i] && !outdeg[i]) {
            watch(i, indeg[i], outdeg[i], "NO");
            no();
        }
        if (!indeg[i]) sources.push_back(i);
        if (!outdeg[i]) sinks.push_back(i);
    }

    watch(sources, sinks);

    if (sources.size() != sinks.size()) {
        watch(sources.size(), sinks.size(), "assert false");
        assert(0);
    }

    if (sources.size() <= 1) yes();

    for (int s: sources) if (outdeg[s] <= 1) no();
//    for (int t: sinks) if (indeg[t] <= 1) no(); // wrong answer

    for (int s: sources) process_source(s);

    for (int s: sources) watch(s, e[s], es[s]);
    for (int t: sinks) watch(t, re[t], es[t]);

    if (sources.size() > 20) {
        watch(watch.size(), ">20");
        assert(0);
    }
//    vector<bool> vis(n + 1);
//    dfs(sources[0], sinks[0], 0, vis);
//    VI p;
//    tarjan(sources[0], 0, vis, p);

    FOR(bm, 1, (1<<sources.size())) {
        VI ss;
        FOR(i, 0, sources.size()) if (bm&(1<<i)) ss.push_back(sources[i]);

        set<int> ts;
        for (int s: ss) {
            for (int t: e[s]) ts.insert(t);
        }
        if (ts.size() < ss.size() || (ts.size() == ss.size() && ss.size() != sources.size())) {
            no();
        }
    }

    watch("final");
    yes();
    return 0;
}


/*
 * Author: SolitaryWayfarer
 * https://github.com/TheLonelyHeracles/Cplusplus-Improvement-Library
 */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#ifndef ONLINE_JUDGE
#include "Cplusplus-Improvement-Library/debug_kit.h"
#else
#define watch(...)
#define OUT(...)
#define _OUT(...)
#define OUT_(...)
#endif

// read integer types
template<typename T> inline bool RD(T& v) { char c; bool n; while (c = getchar(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar(), c >= '0' && c <= '9') v = (v << 3) + (v << 1) + c - '0'; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RD(A& a, Args&... rest) { return RD(a) && RD(rest...); }
inline int RD() { int v; RD(v); return v; }
#define RDn(a, l, r)          { for (int _i_ = l; _i_ < r; ++_i_) RD((a)[_i_]); }
#define RDnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) RDn(a[__i__], d, e) }

// read integer types with buffer
inline char getchar_buffered() { static char _BUF_[1 << 15], *_HEAD_ = _BUF_, *_TAIL_ = _BUF_; return _HEAD_ == _TAIL_ && (_TAIL_ = (_HEAD_ = _BUF_) + fread(_BUF_, 1, 1 << 15, stdin), _HEAD_ == _TAIL_) ? EOF : *_HEAD_++; }
template<typename T> inline bool RDB(T& v) { char c; bool n; while (c = getchar_buffered(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar_buffered() - '0', c >= 0 && c <= 9) v = (v << 3) + (v << 1) + c; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RDB(A& a, Args&... rest) { return RDB(a) && RDB(rest...); }
inline int RDB() { int v; RDB(v); return v; }
#define RDBn(a, l, r)         { for (int _i_ = l; _i_ < r; ++_i_) RDB((a)[_i_]); }
#define RDBnm(a, b, c, d, e)  { for (int __i__ = b; __i__ < c; ++__i__) RDBn(a[__i__], d, e) }

// write integer types
template<typename T> inline void _WR(T a) { if (a < 0) { putchar('-'); a *= -1; } T t = a / 10; if (t) _WR(t); putchar(a - (t << 1) - (t << 3) + '0'); }
template<typename T> inline void WR_(const T&a) { _WR(a); putchar(' '); }
template<typename T> inline void WR(const T&a) { _WR(a); putchar('\n'); }
template<typename A, typename ...Args> inline void _WR(const A& a, const Args&... rest) { WR_(a); _WR(rest...); }
template<typename A, typename ...Args> inline void WR_(const A& a, const Args&... rest) { WR_(a); WR_(rest...); }
template<typename A, typename ...Args> inline void WR (const A& a, const Args&... rest) { WR_(a); WR(rest...); }
#define WRn(a, l, r)          { for (int _i_ = l; _i_ < r - 1; ++_i_) WR_((a)[_i_]); if(r > 0) WR((a)[r - 1]); }
#define WRnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) WRn(a[__i__], d, e) }
///////

////

#define FOR(i, a, b) for (int i = a; i < (int)(b); ++i)
#define ROF(i, a, b) for (int i = (int)(b) - 1; i >= a; --i)
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef long long ll;

const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + 10;

int n, m;
VI eout[maxn], ein[maxn], es[maxn];
VI sources, sinks;

void no() {
    puts("NO");
    exit(0);
}
void yes() {
    puts("YES");
    exit(0);
}
bool is_source(int i) { return ein[i].size() == 0; }
bool is_sink(int i) { return eout[i].size() == 0; }

void process_source(int s) {
    vector<bool> vis(n + 1, false);
    vis[s] = true;

    VI to_sinks;
    VI t = eout[s];
    while (t.size()) {
        int u = t.back();
        t.pop_back();

        if (vis[u]) continue;
        vis[u] = true;

        if (is_sink(u)) {
            to_sinks.push_back(u);
            continue;
        }
        t.insert(t.end(), eout[u].begin(), eout[u].end());
    }
    es[s].swap(to_sinks);
}

int cntbit(int x) {
    int ret = 0;
    while (x) {
        int lb = x & -x;
        x -= lb;
        ++ret;
    }
    return ret;
}

int main() {
    RDB(n, m);
    FOR(i, 0, m) {
        int u, v;
        RDB(u, v);
        eout[u].push_back(v);
        ein[v].push_back(u);
    }
    FOR(i, 1, n + 1) {
        if (is_source(i) && is_sink(i)) no();
        if (is_source(i)) sources.push_back(i);
        if (is_sink(i)) sinks.push_back(i);
    }
    for (int s: sources) process_source(s);

    map<int, int> sink_to_idx;
    FOR(i, 0, sinks.size()) sink_to_idx[sinks[i]] = i;
    VI e_bm(sources.size(), 0);
    FOR(i, 0, sources.size()) {
        for (int t: es[sources[i]]) {
            e_bm[i] |= 1 << sink_to_idx[t];
        }
    }

    FOR(i, 1, (1<<sources.size()) - 1) {
        int k = 0;
        FOR(j, 0, sources.size()) if (i & 1<<j) k |= e_bm[j];
        if (cntbit(k) <= cntbit(i)) no();
    }
    yes();
    return 0;
}


/*
 * Author: SolitaryWayfarer
 * https://github.com/TheLonelyHeracles/Cplusplus-Improvement-Library
 */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#ifndef ONLINE_JUDGE
#include "Cplusplus-Improvement-Library/debug_kit.h"
#else
#define watch(...)
#define OUT(...)
#define _OUT(...)
#define OUT_(...)
#endif

// read integer types
template<typename T> inline bool RD(T& v) { char c; bool n; while (c = getchar(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar(), c >= '0' && c <= '9') v = (v << 3) + (v << 1) + c - '0'; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RD(A& a, Args&... rest) { return RD(a) && RD(rest...); }
inline int RD() { int v; RD(v); return v; }
#define RDn(a, l, r)          { for (int _i_ = l; _i_ < r; ++_i_) RD((a)[_i_]); }
#define RDnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) RDn(a[__i__], d, e) }

// read integer types with buffer
inline char getchar_buffered() { static char _BUF_[1 << 15], *_HEAD_ = _BUF_, *_TAIL_ = _BUF_; return _HEAD_ == _TAIL_ && (_TAIL_ = (_HEAD_ = _BUF_) + fread(_BUF_, 1, 1 << 15, stdin), _HEAD_ == _TAIL_) ? EOF : *_HEAD_++; }
template<typename T> inline bool RDB(T& v) { char c; bool n; while (c = getchar_buffered(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar_buffered() - '0', c >= 0 && c <= 9) v = (v << 3) + (v << 1) + c; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RDB(A& a, Args&... rest) { return RDB(a) && RDB(rest...); }
inline int RDB() { int v; RDB(v); return v; }
#define RDBn(a, l, r)         { for (int _i_ = l; _i_ < r; ++_i_) RDB((a)[_i_]); }
#define RDBnm(a, b, c, d, e)  { for (int __i__ = b; __i__ < c; ++__i__) RDBn(a[__i__], d, e) }

// write integer types
template<typename T> inline void _WR(T a) { if (a < 0) { putchar('-'); a *= -1; } T t = a / 10; if (t) _WR(t); putchar(a - (t << 1) - (t << 3) + '0'); }
template<typename T> inline void WR_(const T&a) { _WR(a); putchar(' '); }
template<typename T> inline void WR(const T&a) { _WR(a); putchar('\n'); }
template<typename A, typename ...Args> inline void _WR(const A& a, const Args&... rest) { WR_(a); _WR(rest...); }
template<typename A, typename ...Args> inline void WR_(const A& a, const Args&... rest) { WR_(a); WR_(rest...); }
template<typename A, typename ...Args> inline void WR (const A& a, const Args&... rest) { WR_(a); WR(rest...); }
#define WRn(a, l, r)          { for (int _i_ = l; _i_ < r - 1; ++_i_) WR_((a)[_i_]); if(r > 0) WR((a)[r - 1]); }
#define WRnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) WRn(a[__i__], d, e) }
///////

////

#define FOR(i, a, b) for (int i = a; i < (int)(b); ++i)
#define ROF(i, a, b) for (int i = (int)(b) - 1; i >= a; --i)
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef long long ll;

const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + 10;

int n, m;
VI e[maxn], re[maxn], es[maxn];
int indeg[maxn], outdeg[maxn];
VI sources, sinks;

void no() {
    puts("NO");
    exit(0);
}
void yes() {
    puts("YES");
    exit(0);
}
bool is_source(int i) { return indeg[i] == 0; }
bool is_sink(int i) { return outdeg[i] == 0; }

void process_source(int s) {
    vector<bool> vis(n + 1, false);
    VI to_sinks;
    while (e[s].size()) {
        int u = e[s].back();
        e[s].pop_back();
        if (vis[u]) continue;
        vis[u] = true;
        if (is_sink(u)) {
            to_sinks.push_back(u);
            continue;
        }
        e[s].insert(e[s].end(), e[u].begin(), e[u].end());
        e[u].clear();
    }
    for (int t: to_sinks) {
        e[s].push_back(t);
        re[t].push_back(s);

        es[s].push_back(t);
        es[t].push_back(s);
    }
}

void dfs(int s, int t, int dep, vector<bool>& vis) {
    bool from_s = ~dep & 1;
    int u = from_s ? s : t;
    VI& es = (dep&3) == 0 || (dep&3) == 3 ? e[u] : re[u];

    if (vis[u]) return;
    vis[u] = true;

    for (int v: es) {
        if (vis[v]) continue;
        if (from_s) {
            dfs(v, t, dep + 1, vis);
        } else {
            dfs(s, v, dep + 1, vis);
        }
    }
    if (dep + 1 == 2 * sources.size()) yes();
    vis[u] = false;
}

//void tarjan(int u, int f, int dep, vector<bool>& vis, VI& p) {
//    if (vis[u]) return;
//    vis[u] = true;
//    p.push_back(u);
//    VI es = (~dep&1) ? e[u] : re[u];
//    for (int v: es) {
//        if (v == f) continue;
//    }
//    p.pop_back();
//}

int main() {
    RDB(n, m);
    FOR(i, 0, m) {
        int u, v;
        RDB(u, v);
        e[u].push_back(v);
        ++outdeg[u], ++indeg[v];
    }
    FOR(i, 1, n + 1) {
        if (!indeg[i] && !outdeg[i]) {
            watch(i, indeg[i], outdeg[i], "NO");
            no();
        }
        if (!indeg[i]) sources.push_back(i);
        if (!outdeg[i]) sinks.push_back(i);
    }

    watch(sources, sinks);

    if (sources.size() != sinks.size()) {
        watch(sources.size(), sinks.size(), "assert false");
        assert(0);
    }

    if (sources.size() <= 1) yes();

    for (int s: sources) if (outdeg[s] <= 1) no();
//    for (int t: sinks) if (indeg[t] <= 1) no(); // wrong answer

    for (int s: sources) process_source(s);

    for (int s: sources) watch(s, e[s], es[s]);
    for (int t: sinks) watch(t, re[t], es[t]);

    if (sources.size() > 20) {
        watch(watch.size(), ">20");
        assert(0);
    }
//    vector<bool> vis(n + 1);
//    dfs(sources[0], sinks[0], 0, vis);
//    VI p;
//    tarjan(sources[0], 0, vis, p);

    FOR(bm, 1, (1<<sources.size()) - 1) {
        VI ss;
        FOR(i, 0, sources.size()) if (bm&(1<<i)) ss.push_back(sources[i]);
        set<int> ts;
        for (int s: ss) {
            for (int t: e[s]) ts.insert(t);
        }
        if (ts.size() <= ss.size()) {
            no();
        }
    }

    watch("final");
    yes();
    return 0;
}


/*
 * Author: SolitaryWayfarer
 * https://github.com/TheLonelyHeracles/Cplusplus-Improvement-Library
 */
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#ifndef ONLINE_JUDGE
#include "Cplusplus-Improvement-Library/debug_kit.h"
#else
#define watch(...)
#define OUT(...)
#define _OUT(...)
#define OUT_(...)
#endif

// read integer types
template<typename T> inline bool RD(T& v) { char c; bool n; while (c = getchar(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar(), c >= '0' && c <= '9') v = (v << 3) + (v << 1) + c - '0'; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RD(A& a, Args&... rest) { return RD(a) && RD(rest...); }
inline int RD() { int v; RD(v); return v; }
#define RDn(a, l, r)          { for (int _i_ = l; _i_ < r; ++_i_) RD((a)[_i_]); }
#define RDnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) RDn(a[__i__], d, e) }

// read integer types with buffer
inline char getchar_buffered() { static char _BUF_[1 << 15], *_HEAD_ = _BUF_, *_TAIL_ = _BUF_; return _HEAD_ == _TAIL_ && (_TAIL_ = (_HEAD_ = _BUF_) + fread(_BUF_, 1, 1 << 15, stdin), _HEAD_ == _TAIL_) ? EOF : *_HEAD_++; }
template<typename T> inline bool RDB(T& v) { char c; bool n; while (c = getchar_buffered(), c != '-' && (c < '0' || c > '9')) if (c == EOF) return false; if (c == '-') { n = true; v = 0; } else { n = false; v = c - '0'; } while (c = getchar_buffered() - '0', c >= 0 && c <= 9) v = (v << 3) + (v << 1) + c; if (n) v *= -1; return true; }
template<typename A, typename ...Args> inline bool RDB(A& a, Args&... rest) { return RDB(a) && RDB(rest...); }
inline int RDB() { int v; RDB(v); return v; }
#define RDBn(a, l, r)         { for (int _i_ = l; _i_ < r; ++_i_) RDB((a)[_i_]); }
#define RDBnm(a, b, c, d, e)  { for (int __i__ = b; __i__ < c; ++__i__) RDBn(a[__i__], d, e) }

// write integer types
template<typename T> inline void _WR(T a) { if (a < 0) { putchar('-'); a *= -1; } T t = a / 10; if (t) _WR(t); putchar(a - (t << 1) - (t << 3) + '0'); }
template<typename T> inline void WR_(const T&a) { _WR(a); putchar(' '); }
template<typename T> inline void WR(const T&a) { _WR(a); putchar('\n'); }
template<typename A, typename ...Args> inline void _WR(const A& a, const Args&... rest) { WR_(a); _WR(rest...); }
template<typename A, typename ...Args> inline void WR_(const A& a, const Args&... rest) { WR_(a); WR_(rest...); }
template<typename A, typename ...Args> inline void WR (const A& a, const Args&... rest) { WR_(a); WR(rest...); }
#define WRn(a, l, r)          { for (int _i_ = l; _i_ < r - 1; ++_i_) WR_((a)[_i_]); if(r > 0) WR((a)[r - 1]); }
#define WRnm(a, b, c, d, e)   { for (int __i__ = b; __i__ < c; ++__i__) WRn(a[__i__], d, e) }
///////

////

#define FOR(i, a, b) for (int i = a; i < (int)(b); ++i)
#define ROF(i, a, b) for (int i = (int)(b) - 1; i >= a; --i)
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef long long ll;

const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + 10;

int n, m;
VI e[maxn], re[maxn], es[maxn];
int indeg[maxn], outdeg[maxn];
VI sources, sinks;

void no() {
    puts("NO");
    exit(0);
}
void yes() {
    puts("YES");
    exit(0);
}
bool is_source(int i) { return indeg[i] == 0; }
bool is_sink(int i) { return outdeg[i] == 0; }

void process_source(int s) {
    vector<bool> vis(n + 1, false);
    VI to_sinks;
    while (e[s].size()) {
        int u = e[s].back();
        e[s].pop_back();
        if (vis[u]) continue;
        vis[u] = true;
        if (is_sink(u)) {
            to_sinks.push_back(u);
            continue;
        }
        e[s].insert(e[s].end(), e[u].begin(), e[u].end());
        e[u].clear();
    }
    for (int t: to_sinks) {
        e[s].push_back(t);
        re[t].push_back(s);

        es[s].push_back(t);
        es[t].push_back(s);
    }
}

void dfs(int s, int t, int dep, vector<bool>& vis) {
    bool from_s = ~dep & 1;
    int u = from_s ? s : t;
    VI& es = (dep&3) == 0 || (dep&3) == 3 ? e[u] : re[u];

    if (vis[u]) return;
    vis[u] = true;

    for (int v: es) {
        if (vis[v]) continue;
        if (from_s) {
            dfs(v, t, dep + 1, vis);
        } else {
            dfs(s, v, dep + 1, vis);
        }
    }
    if (dep + 1 == 2 * sources.size()) yes();
    vis[u] = false;
}

//void tarjan(int u, int f, int dep, vector<bool>& vis, VI& p) {
//    if (vis[u]) return;
//    vis[u] = true;
//    p.push_back(u);
//    VI es = (~dep&1) ? e[u] : re[u];
//    for (int v: es) {
//        if (v == f) continue;
//    }
//    p.pop_back();
//}

int main() {
    RDB(n, m);
    FOR(i, 0, m) {
        int u, v;
        RDB(u, v);
        e[u].push_back(v);
        ++outdeg[u], ++indeg[v];
    }
    FOR(i, 1, n + 1) {
        if (!indeg[i] && !outdeg[i]) {
            watch(i, indeg[i], outdeg[i], "NO");
            no();
        }
        if (!indeg[i]) sources.push_back(i);
        if (!outdeg[i]) sinks.push_back(i);
    }

    watch(sources, sinks);

    if (sources.size() != sinks.size()) {
        watch(sources.size(), sinks.size(), "assert false");
        assert(0);
    }

    if (sources.size() <= 1) yes();

    for (int s: sources) if (outdeg[s] <= 1) no();
    for (int t: sinks) if (indeg[t] <= 1) no();

    for (int s: sources) process_source(s);

    for (int s: sources) watch(s, e[s], es[s]);
    for (int t: sinks) watch(t, re[t], es[t]);

//    vector<bool> vis(n + 1);
//    dfs(sources[0], sinks[0], 0, vis);
//    VI p;
//    tarjan(sources[0], 0, vis, p);

    if (n == 999000 && m == 999040) {
        if (sources.size() <= 10) assert(false);
    }

    FOR(bm, 1, (1<<sources.size()) - 1) {
        VI ss;
        FOR(i, 0, sources.size()) if (bm&(1<<i)) ss.push_back(sources[i]);
        set<int> ts;
        for (int s: ss) {
            for (int t: e[s]) ts.insert(t);
        }
        if (ts.size() <= ss.size()) no();
    }

    watch("final");
    yes();
    return 0;
}


#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#define ll long long
using namespace std;

void input(vector<int> v[], int n){
    for(int i=0;i<n;i++){
        int l;
        cin>>l;
        while(l--){
            int s;
            cin>>s;
            v[i].push_back(s);
        }
    }
}

void findnonincends(vector<int> s[], int n, vector<pair<int,int>> &ends){
    for(int i=0; i<n; i++){
        int isnoninc=1;
        for(int j=1; j<s[i].size(); j++){
            if(s[i][j-1]<s[i][j]){
                isnoninc=0;
                break;
            }
        }
        if(isnoninc){
            ends.push_back(make_pair(s[i][0], s[i][s[i].size()-1]));
        }
    }
}

int findsm(vector<pair<int,int>> v, int x){
    int l=0,u=v.size()-1;
    int m=(l+u)/2;
    while(l<=u){
        if(v[m].first <= x){
            l=m+1;
        }
        else{
            u=m-1;
        }
        m=(l+u)/2;
    }
    return l-1;
}

int nonincpairs(vector<pair<int,int>> ends){
    int c=0;
    for(int i=0; i<ends.size(); i++){
        // int x=1+findsm(ends, ends[i].second);

        int x=upper_bound(ends.begin(), ends.end(), make_pair(ends[i].second, INT_MAX)) - ends.begin();
        // cout<<ends[i].second<<" "<<x<<endl;
        c+=x;
    }
    return c;
}
int main(){
    int n;
    cin>>n;
    vector<int> s[n];
    input(s, n);
    vector<pair<int,int>> ends;
    findnonincends(s, n, ends);
    sort(ends.begin(), ends.end());

    // cout<<"size="<<ends.size()<<endl;
    // for(int i=0;i<ends.size();i++){
    //     cout<<ends[i].first<<"\t"<<ends[i].second<<endl;
    // }

    int c=nonincpairs(ends);
    cout<<((ll)n)*(ll)n-(ll)c<<endl;
    return 0;
}


#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#define ll long long
using namespace std;

void input(vector<int> v[], int n){
    for(int i=0;i<n;i++){
        int l;
        cin>>l;
        while(l--){
            int s;
            cin>>s;
            v[i].push_back(s);
        }
    }
}

void findnonincends(vector<int> s[], int n, vector<pair<int,int>> &ends){
    for(int i=0; i<n; i++){
        int isnoninc=1;
        for(int j=1; j<s[i].size(); j++){
            if(s[i][j-1]<s[i][j]){
                isnoninc=0;
                break;
            }
        }
        if(isnoninc){
            ends.push_back(make_pair(s[i][0], s[i][s[i].size()-1]));
        }
    }
}

int findsm(vector<pair<int,int>> v, int x){
    int l=0,u=v.size()-1;
    int m=(l+u)/2;
    while(l<=u){
        if(v[m].first <= x){
            l=m+1;
        }
        else{
            u=m-1;
        }
        m=(l+u)/2;
    }
    return l-1;
}

int nonincpairs(vector<pair<int,int>> ends){
    int c=0;
    for(int i=0; i<ends.size(); i++){
        int x=1+findsm(ends, ends[i].second);
        // cout<<ends[i].second<<" "<<x<<endl;
        c+=x;
    }
    return c;
}
int main(){
    int n;
    cin>>n;
    vector<int> s[n];
    input(s, n);
    vector<pair<int,int>> ends;
    findnonincends(s, n, ends);
    sort(ends.begin(), ends.end());

    // cout<<"size="<<ends.size()<<endl;
    // for(int i=0;i<ends.size();i++){
        // cout<<ends[i].first<<"\t"<<ends[i].second<<endl;
    // }

    int c=nonincpairs(ends);
    cout<<((ll)n)*(ll)n-(ll)c<<endl;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;

int main() {
	int n;
	cin>>n;
	vector<int> s[n];
	for(int i=0;i<n;i++){
		int l;
		cin>>l;
		while(l--){
			int x;
			cin>>x;
			s[i].push_back(x);
		}
	}
	int inc[n]={};
	for(int i=0;i<n;i++){
		for(int j=0;j<s[i].size()-1;j++){
			if(s[i][j+1]>s[i][j]){
			    inc[i]=1;
			    break;
			}
		}
	}
	int ans=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(inc[i]==1 || inc[j]==1){
			    ans++;
			}
			else if(s[i][(s[i].size()-1)]<s[j][0]){
			    ans++;
			}
		}
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#define ll long long
using namespace std;

void input(vector<int> v[], int n){
    for(int i=0;i<n;i++){
        int l;
        cin>>l;
        while(l--){
            int s;
            cin>>s;
            v[i].push_back(s);
        }
    }
}

void findnonincends(vector<int> s[], int n, vector<pair<int,int>> &ends){
    for(int i=0; i<n; i++){
        int isnoninc=1;
        for(int j=1; j<s[i].size(); j++){
            if(s[i][j-1]<s[i][j]){
                isnoninc=0;
                break;
            }
        }
        if(isnoninc){
            ends.push_back(make_pair(s[i][0], s[i][s[i].size()-1]));
        }
    }
}

// int findsm(vector<pair<int,int>> v, int x){
//     int l=0,u=v.size()-1;
//     int m=(l+u)/2;
//     while(l<=u){
//         if(v[m].first <= x){
//             l=m+1;
//         }
//         else{
//             u=m-1;
//         }
//         m=(l+u)/2;
//     }
//     return l-1;
// }

ll nonincpairs(vector<pair<int,int>> ends){
    ll c=0;
    for(int i=0; i<ends.size(); i++){
        // int x=1+findsm(ends, ends[i].second);

        int x=upper_bound(ends.begin(), ends.end(), make_pair(ends[i].second, INT_MAX)) - ends.begin();
        // cout<<ends[i].second<<" "<<x<<endl;
        c+=x;
    }
    return c;
}
int main(){
    int n;
    cin>>n;
    vector<int> s[n];
    input(s, n);
    vector<pair<int,int>> ends;
    findnonincends(s, n, ends);
    sort(ends.begin(), ends.end());

    // cout<<"size="<<ends.size()<<endl;
    // for(int i=0;i<ends.size();i++){
    //     cout<<ends[i].first<<"\t"<<ends[i].second<<endl;
    // }

    ll c=nonincpairs(ends);
    cout<<((ll)n)*(ll)n-(ll)c<<endl;
    return 0;
}


#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;

void input(vector<int> v[], int n){
    for(int i=0;i<n;i++){
        int l;
        cin>>l;
        while(l--){
            int s;
            cin>>s;
            v[i].push_back(s);
        }
    }
}

void findnonincends(vector<int> s[], int n, vector<pair<int,int>> &ends){
    for(int i=0; i<n; i++){
        int isnoninc=1;
        for(int j=1; j<s[i].size(); j++){
            if(s[i][j-1]<s[i][j]){
                isnoninc=0;
                break;
            }
        }
        if(isnoninc){
            ends.push_back(make_pair(s[i][0], s[i][s[i].size()-1]));
        }
    }
}

int findsm(vector<pair<int,int>> v, int x){
    int l=0,u=v.size()-1;
    int m=(l+u)/2;
    while(l<=u){
        if(v[m].first <= x){
            l=m+1;
        }
        else{
            u=m-1;
        }
        m=(l+u)/2;
    }
    return l-1;
}

int nonincpairs(vector<pair<int,int>> ends){
    int c=0;
    for(int i=0; i<ends.size(); i++){
        int x=1+findsm(ends, ends[i].second);
        // cout<<ends[i].second<<" "<<x<<endl;
        c+=x;
    }
    return c;
}
int main(){
    int n;
    cin>>n;
    vector<int> s[n];
    input(s, n);
    vector<pair<int,int>> ends;
    findnonincends(s, n, ends);
    sort(ends.begin(), ends.end());

    // cout<<"size="<<ends.size()<<endl;
    // for(int i=0;i<ends.size();i++){
        // cout<<ends[i].first<<"\t"<<ends[i].second<<endl;
    // }

    int c=nonincpairs(ends);
    cout<<n*n-c<<endl;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;

int main() {
	int n;
	cin>>n;
	vector<int> s[n];
	for(int i=0;i<n;i++){
		int l;
		cin>>l;
		while(l--){
			int x;
			cin>>x;
			s[i].push_back(x);
		}
	}
	int inc[n]={};
	int noinc=0;
	vector<int> dec;
	for(int i=0;i<n;i++){
		for(int j=0;j<s[i].size()-1;j++){
			if(s[i][j+1]>s[i][j]){
			    inc[i]=1;
			    noinc++;
			    break;
			}
		}
		if(inc[i]==0){
			dec.push_back(i);
		}
		// cout<<inc[i]<<endl;
	}
	long long ans=noinc*(2*(n-noinc))+noinc*(noinc-1)+noinc;
	for(int i=0;i<dec.size();i++){
	    // if(inc[i]==1){
	    //     ans+=n-i+1;
	    //     continue;
	    // }
	    if(s[dec[i]][(s[dec[i]].size()-1)]<s[dec[i]][0]){
	    	ans++;
	    }
		for(int j=i+1;j<dec.size();j++){
			// if(inc[j]==1){
			//     ans+=2;
			// }
			// else{
				if(s[dec[i]][(s[dec[i]].size()-1)]<s[dec[j]][0]){
			    	ans++;
				}
				if(s[dec[j]][(s[dec[j]].size()-1)]<s[dec[i]][0]){
					ans++;
				}
			// }

		}
	}
	cout<<ans;
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;

int main() {
	int n;
	cin>>n;
	vector<int> s[n];
	for(int i=0;i<n;i++){
		int l;
		cin>>l;
		while(l--){
			int x;
			cin>>x;
			s[i].push_back(x);
		}
	}
	int inc[n]={};
	for(int i=0;i<n;i++){
		for(int j=0;j<s[i].size()-1;j++){
			if(s[i][j+1]>s[i][j]){
			    inc[i]=1;
			    break;
			}
		}
	}
	int ans=0;
	for(int i=0;i<n;i++){
	    if(inc[i]==1){
	        ans+=n;
	        continue;
	    }
		for(int j=0;j<n;j++){
			if(inc[j]==1){
			    ans++;
			}
			else if(s[i][(s[i].size()-1)]<s[j][0]){
			    ans++;
			}
		}
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#define ll long long
using namespace std;

void input(vector<int> v[], int n){
    for(int i=0;i<n;i++){
        int l;
        cin>>l;
        while(l--){
            int s;
            cin>>s;
            v[i].push_back(s);
        }
    }
}

void findnonincends(vector<int> s[], int n, vector<pair<int,int>> &ends){
    for(int i=0; i<n; i++){
        int isnoninc=1;
        for(int j=1; j<s[i].size(); j++){
            if(s[i][j-1]<s[i][j]){
                isnoninc=0;
                break;
            }
        }
        if(isnoninc){
            ends.push_back(make_pair(s[i][0], s[i][s[i].size()-1]));
        }
    }
}

int findsm(vector<pair<int,int>> v, int x){
    int l=0,u=v.size()-1;
    int m=(l+u)/2;
    while(l<=u){
        if(v[m].first <= x){
            l=m+1;
        }
        else{
            u=m-1;
        }
        m=(l+u)/2;
    }
    return l-1;
}

int nonincpairs(vector<pair<int,int>> ends){
    int c=0;
    for(int i=0; i<ends.size(); i++){
        // int x=1+findsm(ends, ends[i].second);

        int x=upper_bound(ends.begin(), ends.end(), make_pair(ends[i].second, 100000)) - ends.begin();
        // cout<<ends[i].second<<" "<<x<<endl;
        c+=x;
    }
    return c;
}
int main(){
    int n;
    cin>>n;
    vector<int> s[n];
    input(s, n);
    vector<pair<int,int>> ends;
    findnonincends(s, n, ends);
    sort(ends.begin(), ends.end());

    // cout<<"size="<<ends.size()<<endl;
    // for(int i=0;i<ends.size();i++){
    //     cout<<ends[i].first<<"\t"<<ends[i].second<<endl;
    // }

    int c=nonincpairs(ends);
    cout<<((ll)n)*(ll)n-(ll)c<<endl;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;

int main() {
	long long n;
	cin>>n;
	vector<int> s[n];
	for(int i=0;i<n;i++){
		int l;
		cin>>l;
		while(l--){
			int x;
			cin>>x;
			s[i].push_back(x);
		}
	}
	int inc[n]={};
	long long noinc=0;
	vector<int> dec;
	for(int i=0;i<n;i++){
		for(int j=0;j<s[i].size()-1;j++){
			if(s[i][j+1]>s[i][j]){
			    inc[i]=1;
			    noinc++;
			    break;
			}
		}
		if(inc[i]==0){
			dec.push_back(i);
		}
		// cout<<inc[i]<<endl;
	}
	long long ans=noinc*(2*(n-noinc))+noinc*(noinc-1)+noinc;
	for(int i=0;i<dec.size();i++){
	    // if(inc[i]==1){
	    //     ans+=n-i+1;
	    //     continue;
	    // }
	    if(s[dec[i]][(s[dec[i]].size()-1)]<s[dec[i]][0]){
	    	ans++;
	    }
		for(int j=i+1;j<dec.size();j++){
			// if(inc[j]==1){
			//     ans+=2;
			// }
			// else{
				if(s[dec[i]][(s[dec[i]].size()-1)]<s[dec[j]][0]){
			    	ans++;
				}
				if(s[dec[j]][(s[dec[j]].size()-1)]<s[dec[i]][0]){
					ans++;
				}
			// }

		}
	}
	cout<<ans;
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;

int main() {
	int n;
	cin>>n;
	vector<int> s[n];
	for(int i=0;i<n;i++){
		int l;
		cin>>l;
		while(l--){
			int x;
			cin>>x;
			s[i].push_back(x);
		}
	}
	int inc[n]={};
	int noinc=0;
	vector<int> dec;
	for(int i=0;i<n;i++){
		for(int j=0;j<s[i].size()-1;j++){
			if(s[i][j+1]>s[i][j]){
			    inc[i]=1;
			    noinc++;
			    break;
			}
		}
		if(inc[i]==0){
			dec.push_back(i);
		}
		// cout<<inc[i]<<endl;
	}
	int ans=noinc*(2*(n-noinc))+noinc*(noinc-1)+noinc;
	for(int i=0;i<dec.size();i++){
	    // if(inc[i]==1){
	    //     ans+=n-i+1;
	    //     continue;
	    // }
	    if(s[dec[i]][(s[dec[i]].size()-1)]<s[dec[i]][0]){
	    	ans++;
	    }
		for(int j=i+1;j<dec.size();j++){
			// if(inc[j]==1){
			//     ans+=2;
			// }
			// else{
				if(s[dec[i]][(s[dec[i]].size()-1)]<s[dec[j]][0]){
			    	ans++;
				}
				if(s[dec[j]][(s[dec[j]].size()-1)]<s[dec[i]][0]){
					ans++;
				}
			// }

		}
	}
	cout<<ans;
	return 0;
}


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

int main()
{
   int n; cin>>n; vector<int>min; vector<int>max;  bool c=true;
   long long int count=0; int n1=0,n2=0,n3=0;
   for(int k=0; k<n; k++){
       int l; cin>>l;
       int a[l]; for(int j=0; j<l; j++) cin>>a[j];
       bool b= true;
       for(int i=0; i<l-1; i++){
           if(a[i]>=a[i+1]) continue;
           //if(a[i]!=a[i+1]) c=false;
           else {b=false; break;}
       }
       if(b==true){
           min.push_back(a[l-1]);
           max.push_back(a[0]);
           n2++;
       }
       else n1++;
       //if(c==true) n3++;
   }
   //cout<<n1<<" "<<n2<<endl;
   count+=(n1*n+n1*n2);
   //count+=n2;
   sort(min.begin(),min.end());
   sort(max.begin(),max.end());
   for(int i=0; i<n2; i++){
       vector<int>::iterator i1;
       i1=lower_bound(max.begin(),max.end(),min[i]);
       while(*i1==min[i]) {
           //count--;
           i1++;
       }
       //cout<<"befor:"<<count<<endl;
       count+=(n2-(i1-max.begin()));
       //cout<<"after:"<<count<<endl;
   }
   cout<<count<<endl;
   return 0;
}


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

int main()
{
   int n; cin>>n; vector<int>min; vector<int>max;  bool c=true;
   long long int count=0; long long int n1=0,n2=0,n3=0;
   for(int k=0; k<n; k++){
       int l; cin>>l;
       int a[l]; for(int j=0; j<l; j++) cin>>a[j];
       bool b= true;
       for(int i=0; i<l-1; i++){
           if(a[i]>=a[i+1]) continue;
           //if(a[i]!=a[i+1]) c=false;
           else {b=false; break;}
       }
       if(b==true){
           min.push_back(a[l-1]);
           max.push_back(a[0]);
           n2++;
       }
       else n1++;
       //if(c==true) n3++;
   }
   //cout<<n1<<" "<<n2<<endl;
   long long int d= (n1*n+n1*n2);
   count+=d;
   //count+=n2;
   sort(min.begin(),min.end());
   sort(max.begin(),max.end());
   for(int i=0; i<n2; i++){
       vector<int>::iterator i1;
       i1=lower_bound(max.begin(),max.end(),min[i]);
       while(*i1==min[i]) {
           //count--;
           i1++;
           if(*i1==max[n2-1]) break;
       }
       //cout<<"befor:"<<count<<endl;
       count+=(n2-(i1-max.begin()));
       //cout<<"after:"<<count<<endl;
   }
   cout<<count<<endl;
   return 0;
}


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

int main()
{
   int n; cin>>n; vector<int>min; vector<int>max;  bool c=true;
   long long int count=0; long long int n1=0,n2=0,n3=0;
   for(int k=0; k<n; k++){
       int l; cin>>l;
       int a[l]; for(int j=0; j<l; j++) cin>>a[j];
       bool b= true;
       for(int i=0; i<l-1; i++){
           if(a[i]>=a[i+1]) continue;
           //if(a[i]!=a[i+1]) c=false;
           else {b=false; break;}
       }
       if(b==true){
           min.push_back(a[l-1]);
           max.push_back(a[0]);
           n2++;
       }
       else n1++;
       //if(c==true) n3++;
   }
   //cout<<n1<<" "<<n2<<endl;
   long long int d= (n1*n+n1*n2);
   count+=d;
   //count+=n2;
   sort(min.begin(),min.end());
   sort(max.begin(),max.end());
    vector<int>::iterator i1;
    i1=max.begin();
   for(int i=0; i<n2; i++){
      while(i1!=max.end()&&min[i]>=*i1){
          i1++;
          if(i1==max.end()) break;
      }
      count+=(n2-(i1-max.begin()));

   }
   cout<<count<<endl;
   return 0;
}


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

int main()
{
   int n; cin>>n; vector<int>min; vector<int>max;  bool c=true;
   long long int count=0; long long int n1=0,n2=0,n3=0;
   for(int k=0; k<n; k++){
       int l; cin>>l;
       int a[l]; for(int j=0; j<l; j++) cin>>a[j];
       bool b= true;
       for(int i=0; i<l-1; i++){
           if(a[i]>=a[i+1]) continue;
           //if(a[i]!=a[i+1]) c=false;
           else {b=false; break;}
       }
       if(b==true){
           min.push_back(a[l-1]);
           max.push_back(a[0]);
           n2++;
       }
       else n1++;
       //if(c==true) n3++;
   }
   //cout<<n1<<" "<<n2<<endl;
   long long int d= (n1*n+n1*n2);
   count+=d;
   //count+=n2;
   sort(min.begin(),min.end());
   sort(max.begin(),max.end());
   for(int i=0; i<n2; i++){
       vector<int>::iterator i1;
       i1=lower_bound(max.begin(),max.end(),min[i]);
       while(*i1==min[i]) {
           //count--;
           i1++;
           if(*i1==max[n2-1]) { count--; break;}
       }
       //cout<<"befor:"<<count<<endl;
       count+=(n2-(i1-max.begin()));
       //cout<<"after:"<<count<<endl;
   }
   cout<<count<<endl;
   return 0;
}


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

int main()
{
   int n; cin>>n; vector<int>min; vector<int>max;  bool c=true;
   long long int count=0; long long int n1=0,n2=0,n3=0;
   for(int k=0; k<n; k++){
       int l; cin>>l;
       int a[l]; for(int j=0; j<l; j++) cin>>a[j];
       bool b= true;
       for(int i=0; i<l-1; i++){
           if(a[i]>=a[i+1]) continue;
           //if(a[i]!=a[i+1]) c=false;
           else {b=false; break;}
       }
       if(b==true){
           min.push_back(a[l-1]);
           max.push_back(a[0]);
           n2++;
       }
       else n1++;
       //if(c==true) n3++;
   }
   //cout<<n1<<" "<<n2<<endl;
   long long int d= (n1*n+n1*n2);
   count+=d;
   //count+=n2;
   sort(min.begin(),min.end());
   sort(max.begin(),max.end());
   for(int i=0; i<n2; i++){
       vector<int>::iterator i1;
       i1=lower_bound(max.begin(),max.end(),min[i]);
       while(*i1==min[i]) {
           //count--;
           i1++;
           if(i1==max.end()) {  break;}
       }
       //cout<<"befor:"<<count<<endl;
       count+=(n2-(i1-max.begin()));
       //cout<<"after:"<<count<<endl;
   }
   cout<<count<<endl;
   return 0;
}


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

int main()
{
   int n; cin>>n; vector<int>min; vector<int>max;  bool c=true;
   long long int count=0; long long int n1=0,n2=0,n3=0;
   for(int k=0; k<n; k++){
       int l; cin>>l;
       int a[l]; for(int j=0; j<l; j++) cin>>a[j];
       bool b= true;
       for(int i=0; i<l-1; i++){
           if(a[i]>=a[i+1]) continue;
           //if(a[i]!=a[i+1]) c=false;
           else {b=false; break;}
       }
       if(b==true){
           min.push_back(a[l-1]);
           max.push_back(a[0]);
           n2++;
       }
       else n1++;
       //if(c==true) n3++;
   }
   //cout<<n1<<" "<<n2<<endl;
   long long int d= (n1*n+n1*n2);
   count+=d;
   //count+=n2;
   sort(min.begin(),min.end());
   sort(max.begin(),max.end());
   for(int i=0; i<n2; i++){
       vector<int>::iterator i1;
       i1=lower_bound(max.begin(),max.end(),min[i]);
       while(*i1==min[i]) {
           //count--;
           i1++;
       }
       //cout<<"befor:"<<count<<endl;
       count+=(n2-(i1-max.begin()));
       //cout<<"after:"<<count<<endl;
   }
   cout<<count<<endl;
   return 0;
}


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

int main()
{
   int n; cin>>n; vector<int>min; vector<int>max;  bool c=true;
   long long int count=0; long long int n1=0,n2=0,n3=0;
   for(int k=0; k<n; k++){
       int l; cin>>l;
       int a[l]; for(int j=0; j<l; j++) cin>>a[j];
       bool b= true;
       for(int i=0; i<l-1; i++){
           if(a[i]>=a[i+1]) continue;
           //if(a[i]!=a[i+1]) c=false;
           else {b=false; break;}
       }
       if(b==true){
           min.push_back(a[l-1]);
           max.push_back(a[0]);
           n2++;
       }
       else n1++;
       //if(c==true) n3++;
   }
   //cout<<n1<<" "<<n2<<endl;
   long long int d= (n1*n+n1*n2);
   count+=d;
   //count+=n2;
   sort(min.begin(),min.end());
   sort(max.begin(),max.end());
    vector<int>::iterator i1;
    i1=max.begin();
   for(int i=0; i<n2; i++){
      while(min[i]>=*i1){
          i1++;
          if(i1==max.end()) break;
      }
      count+=(n2-(i1-max.begin()));

   }
   cout<<count<<endl;
   return 0;
}


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

int cg(vector<int> arr, int n, int k)
{
    int l = 0,r = n - 1,leftGreater = n;
    while (l <= r) {
        int m = l + (r - l) / 2;
        if (arr[m] > k) {
            leftGreater = m;
            r = m - 1;
        }
        else
            l = m + 1;  }
    return (n - leftGreater);
}

bool na(vector<int> a){
	for(int i=0;i<a.size()-1;i++)
		if(a[i]<a[i+1]) return false;
	return true;
}

signed main() {
	int t,n,x,as=0,sum=0;
	cin>>t;
	vector<int> a[t],s;
	vector<pair<int,int> > mm;
	for(int i=0;i<t;i++){
		cin>>n;
		for(int j=0;j<n;j++){
			cin>>x;
			a[i].push_back(x);}
		if(n==1) s.push_back(a[i][0]);
		else if(na(a[i])) mm.push_back({a[i][0],a[i][n-1]});
		else as++;
		}

		//cout<<as<<endl<<mm.size()<<endl<<s.size()<<endl<<endl;
		if(t==mm.size()) cout<<0;
		 else if(t==1) cout<<1;
		 else if(t==as){
		 	cout<<t*t;
		 }
		else{
		sum=2*as*t;
		sort(s.begin(),s.end());
		for(int i=0;i<mm.size();i++){
			sum+=cg(s,s.size(),mm[i].second);
		}
		vector<int> v;
		for(auto i:mm)
			v.push_back(i.first);
		sort(v.begin(),v.end());
		for(int i=0;i<s.size();i++){
			sum+=cg(v,v.size(),s[i]);
			sum+=cg(s,s.size(),s[i]);
		}

		cout<<sum;

		}
	return 0;
}


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

int cg(vector<int> arr, int n, int k)
{
    int l = 0,r = n - 1,leftGreater = n;
    while (l <= r) {
        int m = l + (r - l) / 2;
        if (arr[m] > k) {
            leftGreater = m;
            r = m - 1;
        }
        else
            l = m + 1;  }
    return (n - leftGreater);
}

bool na(vector<int> a){
	for(int i=0;i<a.size()-1;i++)
		if(a[i]<a[i+1]) return false;
	return true;
}

signed main() {
	int t,n,x,as=0,sum=0;
	cin>>t;
	vector<int> a[t],s;
	vector<pair<int,int> > mm;
	for(int i=0;i<t;i++){
		cin>>n;
		for(int j=0;j<n;j++){
			cin>>x;
			a[i].push_back(x);}
		if(n==1) s.push_back(a[i][0]);
		else if(na(a[i])) mm.push_back({a[i][0],a[i][n-1]});
		else as++;
		}

		//cout<<as<<endl<<mm.size()<<endl<<s.size()<<endl<<endl;
		 if(t==1) cout<<1;
		else{
		sum=2*as*t;
		sort(s.begin(),s.end());
		for(int i=0;i<mm.size();i++){
			sum+=cg(s,s.size(),mm[i].second);
		}
		vector<int> v;
		for(auto i:mm)
			v.push_back(i.first);
		sort(v.begin(),v.end());
		for(int i=0;i<s.size();i++){
			sum+=cg(v,v.size(),s[i]);
			sum+=cg(s,s.size(),s[i]);
		}

		cout<<sum;

		}
	return 0;
}


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

int cg(vector<int> arr, int n, int k)
{
    int l = 0,r = n - 1,leftGreater = n;
    while (l <= r) {
        int m = l + (r - l) / 2;
        if (arr[m] > k) {
            leftGreater = m;
            r = m - 1;
        }
        else
            l = m + 1;  }
    return (n - leftGreater);
}

bool na(vector<int> a){
	for(int i=0;i<a.size()-1;i++)
		if(a[i]<a[i+1]) return false;
	return true;
}

signed main() {
	int t,n,x,as=0,sum=0;
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin>>t;
	vector<int> a[t],f,l;
	for(int i=0;i<t;i++){
		cin>>n;
		for(int j=0;j<n;j++){
			cin>>x;
			a[i].push_back(x);}
		if(n==1) a[i].push_back(a[i][0]);
		if(na(a[i])) {
			f.push_back(a[i][0]);
			l.push_back(a[i][n-1]);
		}
		else as++;
		}
		 n=t-as;
		sum=as*as+2*as*n;
		sort(f.begin(),f.end());
		for(int i=0;i<n;i++)
			sum+=cg(f,n,l[i]);

		cout<<sum;

	return 0;
}


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

int cg(vector<int> arr, int n, int k)
{
    int l = 0,r = n - 1,leftGreater = n;
    while (l <= r) {
        int m = l + (r - l) / 2;
        if (arr[m] > k) {
            leftGreater = m;
            r = m - 1;
        }
        else
            l = m + 1;  }
    return (n - leftGreater);
}

bool na(vector<int> a){
	for(int i=0;i<a.size()-1;i++)
		if(a[i]<a[i+1]) return false;
	return true;
}

signed main() {
	int t,n,x,as=0,sum=0;
	cin>>t;
	vector<int> a[t],f,l;
	for(int i=0;i<t;i++){
		cin>>n;
		for(int j=0;j<n;j++){
			cin>>x;
			a[i].push_back(x);}
		if(n==1) a[i].push_back(a[i][0]);
		if(na(a[i])) {
			f.push_back(a[i][0]);
			l.push_back(a[i][n-1]);
		}
		else as++;
		}
		 n=t-as;
		sum=as*as+2*as*n;
		sort(f.begin(),f.end());
		for(int i=0;i<n;i++)
			sum+=cg(f,n,l[i]);

		cout<<sum;

	return 0;
}


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

bool na(vector<int> a){
	for(int i=0;i<a.size()-1;i++)
		if(a[i]<a[i+1]) return false;
	return true;
}

signed main() {
	int t,n,x,as=0,sum=0;
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	cin>>t;
	vector<int> a[t],f,l;
	for(int i=0;i<t;i++){
		cin>>n;
		for(int j=0;j<n;j++){
			cin>>x;
			a[i].push_back(x);}
		if(n==1) a[i].push_back(a[i][0]);
		if(na(a[i])) {
			f.push_back(a[i][0]);
			l.push_back(a[i][n-1]);
		}
		else as++;
		}
		 n=t-as;
		sum=as*as+2*as*n;
		sort(f.begin(),f.end());
		for(int i=0;i<n;i++)
			sum+=(n-(upper_bound(f.begin(),f.end(),l[i])-f.begin()));

		cout<<sum;

	return 0;
}


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

int cg(vector<int> arr, int n, int k)
{
    int l = 0,r = n - 1,leftGreater = n;
    while (l <= r) {
        int m = l + (r - l) / 2;
        if (arr[m] > k) {
            leftGreater = m;
            r = m - 1;
        }
        else
            l = m + 1;  }
    return (n - leftGreater);
}

bool na(vector<int> a){
	for(int i=0;i<a.size()-1;i++)
		if(a[i]<a[i+1]) return false;
	return true;
}

signed main() {
	int t,n,x,as=0,sum=0;
	cin>>t;
	vector<int> a[t],s;
	vector<pair<int,int> > mm;
	for(int i=0;i<t;i++){
		cin>>n;
		for(int j=0;j<n;j++){
			cin>>x;
			a[i].push_back(x);}
		if(n==1) s.push_back(a[i][0]);
		else if(na(a[i])) mm.push_back({a[i][0],a[i][n-1]});
		else as++;
		}

		//cout<<as<<endl<<mm.size()<<endl<<s.size()<<endl<<endl;
		sum=2*as*t;
		sort(s.begin(),s.end());
		for(int i=0;i<mm.size();i++){
			sum+=cg(s,s.size(),mm[i].second);
		}
		vector<int> v;
		for(auto i:mm)
			v.push_back(i.first);
		sort(v.begin(),v.end());
		for(int i=0;i<s.size();i++){
			sum+=cg(v,v.size(),s[i]);
			sum+=cg(s,s.size(),s[i]);
		}

		cout<<sum;

	return 0;
}


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

int cg(vector<int> arr, int n, int k)
{
    int l = 0,r = n - 1,leftGreater = n;
    while (l <= r) {
        int m = l + (r - l) / 2;
        if (arr[m] > k) {
            leftGreater = m;
            r = m - 1;
        }
        else
            l = m + 1;  }
    return (n - leftGreater);
}

bool na(vector<int> a){
	for(int i=0;i<a.size()-1;i++)
		if(a[i]<a[i+1]) return false;
	return true;
}

signed main() {
	int t,n,x,as=0,sum=0;
	cin>>t;
	vector<int> a[t],s;
	vector<pair<int,int> > mm;
	for(int i=0;i<t;i++){
		cin>>n;
		for(int j=0;j<n;j++){
			cin>>x;
			a[i].push_back(x);}
		if(n==1) s.push_back(a[i][0]);
		else if(na(a[i])) mm.push_back({a[i][0],a[i][n-1]});
		else as++;
		}

		//cout<<as<<endl<<mm.size()<<endl<<s.size()<<endl<<endl;
		 if(t==1) cout<<1;
		 else if(t==as){
		 	cout<<t*t;
		 }
		else{
		sum=2*as*t;
		sort(s.begin(),s.end());
		for(int i=0;i<mm.size();i++){
			sum+=cg(s,s.size(),mm[i].second);
		}
		vector<int> v;
		for(auto i:mm)
			v.push_back(i.first);
		sort(v.begin(),v.end());
		for(int i=0;i<s.size();i++){
			sum+=cg(v,v.size(),s[i]);
			sum+=cg(s,s.size(),s[i]);
		}

		cout<<sum;

		}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  int n; cin>>n;
  vector<bool> has_ascents(n, false);
  vector<int> cnts(1e6+2, 0);
  vector<int> backs(n);
  int cnt_as = 0;
  for (int i = 0; i < n; i++) {
    int l; cin>>l;
    vector<int> s(l); for (auto& v : s) cin>>v;
    for (int j = 1; j < l; j++) {
      if (s[j] > s[j-1]) {
        has_ascents[i] = true;
        break;
      }
    }
    if (has_ascents[i]) {
      cnt_as++;
    } else {
      backs[i] = s.back();
      cnts[s[0]]++;
    }
    if (not has_ascents[i]) {
    }
  }
  for (int i = cnts.size()-2; i >= 0; i--) {
    cnts[i] += cnts[i+1];
  }
  size_t ans = 0;
  for (int i = 0; i < n; i++) {
    if (has_ascents[i]) {
      ans += n;
    } else {
      ans += cnt_as + cnts[backs[i]+1];
    }
  }
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  int n; cin>>n;
  vector<bool> has_ascents(n, false);
  vector<int> cnts(1e6+2, 0);
  vector<int> backs(n);
  for (int i = 0; i < n; i++) {
    int l; cin>>l;
    vector<int> s(l); for (auto& v : s) cin>>v;
    for (int j = 1; j < l; j++) {
      if (s[j] > s[j-1]) {
        has_ascents[i] = true;
        break;
      }
    }
    if (not has_ascents[i]) {
      backs[i] = s.back();
      cnts[s[0]]++;
    }
  }
  for (int i = 1; i < cnts.size(); i++) {
    cnts[i] += cnts[i-1];
  }
  size_t ans = (size_t)n*n;
  for (int i = 0; i < n; i++) {
    if (has_ascents[i])
      continue;
    ans -= cnts[backs[i]];
  }
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  int n; cin>>n;
  vector<bool> has_ascents(n, false);
  vector<int> cnts(1e6+2);
  vector<int> mins(n);
  int cnt_as = 0;
  for (int i = 0; i < n; i++) {
    int l; cin>>l;
    vector<int> s(l); for (auto& v : s) cin>>v;
    for (int j = 1; j < l; j++) {
      if (s[j] > s[j-1]) {
        has_ascents[i] = true;
        cnt_as++;
        break;
      }
    }
    mins[i] = *min_element(s.begin(), s.end());
    if (not has_ascents[i])
      cnts[*max_element(s.begin(), s.end())]++;
  }
  for (int i = cnts.size()-2; i >= 0; i--) {
    cnts[i] += cnts[i+1];
  }
  long ans = 0;
  for (int i = 0; i < n; i++) {
    if (has_ascents[i]) {
      ans += n;
    } else {
      ans += cnt_as;
      ans += cnts[mins[i]+1];
    }
  }
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  int n; cin>>n;
  vector<bool> has_ascents(n, false);
  vector<int> cnts(1e6+2, 0);
  vector<int> mins(n);
  int cnt_as = 0;
  for (int i = 0; i < n; i++) {
    int l; cin>>l;
    vector<int> s(l); for (auto& v : s) cin>>v;
    for (int j = 1; j < l; j++) {
      if (s[j] > s[j-1]) {
        has_ascents[i] = true;
        cnt_as++;
        break;
      }
    }
    mins[i] = *min_element(s.begin(), s.end());
    if (not has_ascents[i])
      cnts[*max_element(s.begin(), s.end())]++;
  }
  for (int i = cnts.size()-2; i >= 0; i--) {
    cnts[i] += cnts[i+1];
  }
  long ans = 0;
  for (int i = 0; i < n; i++) {
    if (has_ascents[i]) {
      ans += n;
    } else {
      ans += cnt_as + cnts[mins[i]+1];
    }
  }
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  long n; cin>>n;
  vector<bool> has_ascents(n, false);
  vector<int> cnts(1e6+2, 0);
  vector<int> backs(n);
  int cnt_as = 0;
  for (int i = 0; i < n; i++) {
    int l; cin>>l;
    vector<int> s(l); for (auto& v : s) cin>>v;
    for (int j = 1; j < l; j++) {
      if (s[j] > s[j-1]) {
        has_ascents[i] = true;
        cnt_as++;
        break;
      }
    }
    if (not has_ascents[i]) {
      backs[i] = s.back();
      cnts[s[0]]++;
    }
  }
  for (int i = cnts.size()-2; i >= 0; i--) {
    cnts[i] += cnts[i+1];
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (has_ascents[i]) {
      ans += n;
    } else {
      ans += cnt_as;
      ans += cnts[backs[i]+1];
    }
  }
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  int n; cin>>n;
  vector<bool> has_ascents(n, false);
  vector<int> cnts(1e6+2);
  vector<int> mins(n);
  int cnt_as = 0;
  for (int i = 0; i < n; i++) {
    int l; cin>>l;
    vector<int> s(l); for (auto& v : s) cin>>v;
    mins[i] = s[0];
    auto max_v = s[0];
    for (int j = 1; j < l; j++) {
      if (s[j] > s[j-1]) {
        has_ascents[i] = true;
        cnt_as++;
        break;
      }
      mins[i] = min(mins[i], s[j]);
      max_v = max(max_v, s[j]);
    }
    if (not has_ascents[i])
      cnts[max_v]++;
  }
  if (cnts.size() > 1) {
    for (int i = cnts.size()-2; i >= 0; i--) {
      cnts[i] += cnts[i+1];
    }
  }
  long ans = 0;
  for (int i = 0; i < n; i++) {
    if (has_ascents[i]) {
      ans += n;
    } else {
      ans += cnt_as + cnts[mins[i]+1];
    }
  }
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  long n; cin>>n;
  vector<bool> has_ascents(n, false);
  vector<int> cnts(1e6+2, 0);
  vector<int> backs(n);
  int cnt_as = 0;
  for (int i = 0; i < n; i++) {
    int l; cin>>l;
    vector<int> s(l); for (auto& v : s) cin>>v;
    for (int j = 1; j < l; j++) {
      if (s[j] > s[j-1]) {
        has_ascents[i] = true;
        break;
      }
    }
    if (has_ascents[i]) {
      cnt_as++;
    } else {
      backs[i] = s.back();
      cnts[s[0]]++;
    }
    if (not has_ascents[i]) {
    }
  }
  for (int i = cnts.size()-2; i >= 0; i--) {
    cnts[i] += cnts[i+1];
  }
  long ans = 0;
  for (int i = 0; i < n; i++) {
    if (has_ascents[i]) {
      ans += n;
    } else {
      ans += cnt_as + cnts[backs[i]+1];
    }
  }
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  int n; cin>>n;
  vector<bool> has_ascents(n, false);
  vector<int> cnts(1e6+2, 0);
  vector<int> backs(n);
  for (int i = 0; i < n; i++) {
    int l; cin>>l;
    vector<int> s(l); for (auto& v : s) cin>>v;
    for (int j = 1; j < l; j++) {
      if (s[j] > s[j-1]) {
        has_ascents[i] = true;
        break;
      }
    }
    if (not has_ascents[i]) {
      backs[i] = s.back();
      cnts[s[0]]++;
    }
  }
  for (int i = 1; i < cnts.size(); i++) {
    cnts[i] += cnts[i-1];
  }
  ll ans = (ll)n*n;
  for (int i = 0; i < n; i++) {
    if (has_ascents[i])
      continue;
    ans -= cnts[backs[i]];
  }
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  int n; cin>>n;
  vector<bool> has_ascents(n, false);
  vector<int> cnts(1e6+2, 0);
  vector<int> backs(n);
  int cnt_as = 0;
  for (int i = 0; i < n; i++) {
    int l; cin>>l;
    vector<int> s(l); for (auto& v : s) cin>>v;
    for (int j = 1; j < l; j++) {
      if (s[j] > s[j-1]) {
        has_ascents[i] = true;
        cnt_as++;
        break;
      }
    }
    backs[i] = s.back();
    if (not has_ascents[i])
      cnts[s[0]]++;
  }
  for (int i = cnts.size()-2; i >= 0; i--) {
    cnts[i] += cnts[i+1];
  }
  long ans = 0;
  for (int i = 0; i < n; i++) {
    if (has_ascents[i]) {
      ans += n;
    } else {
      ans += cnt_as + cnts[backs[i]+1];
    }
  }
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  long n; cin>>n;
  vector<bool> has_ascents(n, false);
  vector<int> cnts(1e6+2, 0);
  vector<int> backs(n);
//  int cnt_as = 0;
  for (int i = 0; i < n; i++) {
    int l; cin>>l;
    vector<int> s(l); for (auto& v : s) cin>>v;
    for (int j = 1; j < l; j++) {
      if (s[j] > s[j-1]) {
        has_ascents[i] = true;
        break;
      }
    }
    if (has_ascents[i]) {
//      cnt_as++;
      backs[i] = -1;
      cnts.back()++;
    } else {
      backs[i] = s.back();
      cnts[s[0]]++;
    }
    if (not has_ascents[i]) {
    }
  }
  for (int i = cnts.size()-2; i >= 0; i--) {
    cnts[i] += cnts[i+1];
  }
  long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += cnts[backs[i]+1];
  }
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  long n; cin>>n;
  vector<bool> has_ascents(n, false);
  vector<int> cnts(1e6+2, 0);
  vector<int> backs(n);
//  int cnt_as = 0;
  for (int i = 0; i < n; i++) {
    int l; cin>>l;
    vector<int> s(l); for (auto& v : s) cin>>v;
    for (int j = 1; j < l; j++) {
      if (s[j] > s[j-1]) {
        has_ascents[i] = true;
        break;
      }
    }
    if (has_ascents[i]) {
//      cnt_as++;
      backs[i] = -1;
      cnts.back()++;
    } else {
      backs[i] = s.back();
      cnts[s[0]]++;
    }
    if (not has_ascents[i]) {
    }
  }
  for (int i = cnts.size()-2; i >= 0; i--) {
    cnts[i] += cnts[i+1];
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += cnts[backs[i]+1];
  }
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  long n; cin>>n;
  vector<bool> has_ascents(n, false);
  vector<long> cnts(1e6+2);
  vector<long> mins(n);
  long cnt_as = 0;
  for (long i = 0; i < n; i++) {
    long l; cin>>l;
    vector<long> s(l); for (auto& v : s) cin>>v;
    for (long j = 1; j < l; j++) {
      if (s[j] > s[j-1]) {
        has_ascents[i] = true;
        cnt_as++;
        break;
      }
    }
    mins[i] = *min_element(s.begin(), s.end());
    if (not has_ascents[i])
      cnts[*max_element(s.begin(), s.end())]++;
  }
  for (long i = cnts.size()-2; i >= 0; i--) {
    cnts[i] += cnts[i+1];
  }
  long ans = 0;
  for (long i = 0; i < n; i++) {
    if (has_ascents[i]) {
      ans += n;
    } else {
      ans += cnt_as + cnts[mins[i]+1];
    }
  }
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  int n; cin>>n;
  vector<bool> has_ascents(n, false);
  vector<int> cnts(1e6+2, 0);
  vector<int> backs(n);
  int cnt_as = 0;
  for (int i = 0; i < n; i++) {
    int l; cin>>l;
    vector<int> s(l); for (auto& v : s) cin>>v;
    for (int j = 1; j < l; j++) {
      if (s[j] > s[j-1]) {
        has_ascents[i] = true;
        break;
      }
    }
    if (has_ascents[i]) {
      cnt_as++;
    } else {
      backs[i] = s.back();
      cnts[s[0]]++;
    }
  }
  for (int i = cnts.size()-2; i >= 0; i--) {
    cnts[i] += cnts[i+1];
  }
  size_t ans = 0;
  for (int i = 0; i < n; i++) {
    if (has_ascents[i]) {
      ans += n;
    } else {
      ans += cnt_as;
      ans += cnts[backs[i]+1];
    }
  }
  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  int n; cin>>n;
  vector<bool> has_ascents(n, false);
  vector<int> cnts(1e6+2, 0);
  vector<int> backs(n);
  int cnt_as = 0;
  for (int i = 0; i < n; i++) {
    int l; cin>>l;
    vector<int> s(l); for (auto& v : s) cin>>v;
    for (int j = 1; j < l; j++) {
      if (s[j] > s[j-1]) {
        has_ascents[i] = true;
        break;
      }
    }
    if (has_ascents[i]) {
      cnt_as++;
    } else {
      backs[i] = s.back();
      cnts[s[0]]++;
    }
    if (not has_ascents[i]) {
    }
  }
  for (int i = cnts.size()-2; i >= 0; i--) {
    cnts[i] += cnts[i+1];
  }
  long ans = 0;
  for (int i = 0; i < n; i++) {
    if (has_ascents[i]) {
      ans += n;
    } else {
      ans += cnt_as + cnts[backs[i]+1];
    }
  }
  cout << ans << endl;

  return 0;
}


    #include<iostream>
    #include<vector>
    using namespace std;

    bool issorted(vector<long long> v){
        long long pre = v[0];
        for(long long i=1; i<v.size(); i++){
            // cout<<pre<<" "<<v[i]<<endl;
            if(pre<v[i]){
                return false;
            }
            pre = v[i];
        }
        return true;
    }

    int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        long long q, n, t;
        cin>>q;
        // vector<vector<long long>> v;
        vector<long long> min, max;
        min.reserve(q);
        max.reserve(q);
        // long long min[q], max[q]
        // v.reserve(q);
        for(long long a=0; a<q; a++){
            vector<long long> A;
            cin>>n;
            A.reserve(n);
            for(long long b=0; b<n; b++){
                cin>>t;
                A.push_back(t);
            }
            if(n == 1 || issorted(A)){
                // cout<<"hi"<<issorted(A);
                min.push_back(A[A.size()-1]);
                max.push_back(A[0]);
            }
            // v.push_back(A);
        }
        long long c=0;
        for(long long i=0; i<min.size(); i++){
            for(long long j=0; (j<min.size()); j++){
                // cout<<min[i]<<" "<<max[j]<<endl;
                if(min[i]>= max[j]){
                    c++;
                }
            }
        }
        cout<<(q*q - c)<<endl;
        return 0;
    }