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

const int X = 105;
const int mod = 1e9 + 7;

int n,b,k,x;
int ori[X][X], ed[X][X];

int c[X][X];
void selfmul()
{
    memset(c,0,sizeof c);
    for(int i=0;i<x;++i)
        for(int j=0;j<x;++j)
            for(int k=0;k<x;++k)
                c[i][j] = (c[i][j] + ori[i][k]*ori[k][j])%mod;
    for(int i=0;i<x;++i)
        for(int j=0;j<x;++j)
            ori[i][j] = c[i][j];
}

int anstmp[X];
int ans[X];
void mul()
{
    memset(anstmp,0,sizeof anstmp);
    for(int i=0; i<x; ++i)
        for(int k=0; k<x; ++k)
            anstmp[i] = (anstmp[i] + ori[k][i]*ans[k])%mod;
    for(int i=0;i<x;++i) ans[i] = anstmp[i];
}

void makeed(int b)
{
    for(;b;b>>=1, selfmul())
        if(b&1) mul();
}

signed main()
{

    scanf("%lld%lld%lld%lld", &n,&b,&k,&x);
    for(int i=1, al; i<=n; ++i) {
        scanf("%lld", &al);
        for(int j=0; j<x; ++j)
            ++ori[j][(10*j+al)%x];
    }
    ans[0]=1;
    makeed(b);
    cout << ans[k];

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const LL N=105,mod=1e9+7;
LL a[N][N];
LL ans[N];
LL n,b,k,x,sz;
void mul() {
	LL c[N];
	memset(c,0,sizeof c);
	for(int i=0; i<x; i++) {
		for(int j=0; j<x; j++) {
			c[i]=(c[i]+ans[j]*a[j][i])%mod;
		}
	}
	memcpy(ans,c,sizeof ans);
}
void mulself() {
	LL c[N][N];
	memset(c,0,sizeof c);
	for(int i=0; i<x; i++) {
		for(int j=0; j<x; j++) {
			for(int k=0; k<x; k++) {
				c[i][j]=(c[i][j]+a[i][k]*a[k][j])%mod;
			}
		}
	}
	memcpy(a,c,sizeof a);
}
int main() {
	scanf("%lld%lld%lld%lld",&n,&b,&k,&x);
	ans[0]=1;
	for(int i=0; i<n; i++) {
		scanf("%lld",&sz);
		for(int j=0; j<x; j++) {
			a[j][(10*j+sz)%x]++;
		}
	}
	while(b) {
		if(b&1) mul();
		mulself();
		b>>=1;
	}
	printf("%lld\n",ans[k]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define reg register
typedef long long ll;
#define MOD 1000000007
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
static char buf[100000],*p1=buf,*p2=buf;
inline int read(void){
	reg char ch=getchar();
	reg int res=0;
	while(ch<'0'||'9'<ch)ch=getchar();
	while('0'<=ch&&ch<='9')res=10*res+ch-'0',ch=getchar();
	return res;
}

const int MAXSIZE=100;

struct Matrix{
	ll unit[MAXSIZE][MAXSIZE];
	inline Matrix(void){
		memset(unit,0,sizeof(unit));
		return;
	}
	inline Matrix(reg ll x){
		for(reg int i=0;i<MAXSIZE;++i)
			for(reg int j=0;j<MAXSIZE;++j)
				unit[i][j]=(i==j)?x:0;
		return;
	}
	inline ll* operator[](reg int i){
		return unit[i];
	}
	inline Matrix operator*(Matrix &a){
		Matrix res;
		for(reg int i=0;i<MAXSIZE;++i)
			for(reg int j=0;j<MAXSIZE;++j)
				for(reg int k=0;k<MAXSIZE;++k)
					res[i][j]=(res[i][j]+unit[i][k]*a[k][j]%MOD)%MOD;
		return res;
	}
	inline Matrix operator^(reg ll exp){
		Matrix res(1),x=*this;
		while(exp){
			if(exp&1)
				res=res*x;
			x=x*x;
			exp>>=1;
		}
		return res;
	}
};

int n,b,K,x;
int T[10];
Matrix ans;

int main(void){
	n=read(),b=read(),K=read(),x=read();
	for(reg int i=1;i<=n;++i)
		++T[read()];
	for(reg int i=0;i<x;++i)
		for(reg int j=0;j<10;++j)
			ans[i][(10*i+j)%x]+=T[j];
	ans=ans^b;
	printf("%lld\n",ans[0][K]);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 5e4 + 5;
const int maxm = 103;
const int mod = 1e9 + 7;
int a[maxn];
typedef long long ll;
int n, B, K, x;
struct mat {
    ll p[maxm][maxm];

    mat() {

        memset(p, 0, sizeof p);
    }

};
mat operator*(const mat& a, const mat& b) {
    mat c;
    for (int i = 0;i < x;i++) {
        for (int j = 0;j < x;j++) {
            for (int k = 0;k < x;k++) {
                c.p[i][j] = (c.p[i][j] + a.p[i][k] * b.p[k][j] % mod) % mod;
            }
        }
    }
    return c;
}
mat quickpow(mat& a, ll n) {
    mat c;
    for (int i = 0;i < x;i++) {
        c.p[i][i] = 1;
    }
    while (n) {
        if (n & 1) {
            c = c * a;
        }
        a = a * a;
        n >>= 1;
    }
    return c;
}
int main() {

    cin >> n >> B >> K >> x;
    for (int i = 1;i <= n;i++) {
        scanf("%d", &a[i]);
    }
    mat f0;
    for (int i = 1;i <= n;i++) {
        (f0.p[a[i] % x][0])++;
    }
    mat f;
    for (int i = 0;i < x;i++) {
        for (int j = 1;j <= n;j++) {
            f.p[(i * 10 + a[j]) % x][i]++;
        }
    }
    f = quickpow(f, B - 1);
    f = f * f0;
    printf("%lld\n", f.p[K][0]);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#define ll long long
using namespace std;
const int N=5e4+10,M=110;
const ll mod=1e9+7;
struct matrix
{
	int n,m;
	ll v[M][M];
	matrix(){ n=m=0; memset(v,0,sizeof(v)); }
	matrix operator *(const matrix a)const
	{
		matrix ans; ans.n=n,ans.m=a.m;
		for(int i=0;i<ans.n;i++)
			for(int j=0;j<ans.m;j++)
				for(int k=0;k<m;k++)
					(ans.v[i][j]+=v[i][k]*a.v[k][j]%mod)%=mod;
		return ans;
	}
}f,A;
int main()
{
	int n,m,k,x,w;
	scanf("%d%d%d%d",&m,&n,&k,&x);
	f.n=x,f.m=1;
	A.n=A.m=x;
	for(int i=1;i<=m;i++)
	{
		scanf("%d",&w);
		for(int j=0;j<x;j++)
			A.v[j][(j*10+w)%x]++;
		f.v[w%x][0]++;
	}
	if(n==1){ printf("%lld\n",f.v[k][0]); return 0; }
	for(n--;n;n>>=1)
		if(n&1) A=A*A;
	f=f*A;
	printf("%lld",f.v[k][0]);
	return 0;
}


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

#define LL long long
const int maxn=1e5+5;
const int mod=1e9+7;

LL n,b,k,x;
int num[200];
struct Matrix
{
    LL m[105][105];
    void mt()
    {
        memset(m,0,sizeof(m));
    }
};
Matrix Mult(Matrix a,Matrix b)  //矩阵乘法
{
    Matrix ans;
    ans.mt();
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            ans.m[i][j]=0;
            for(int k=0;k<n;k++)
            {
                ans.m[i][j]+=a.m[i][k]*b.m[k][j];
                ans.m[i][j]%=mod;
            }
        }
    }
    return ans;
}
Matrix quickpow(Matrix a,LL b)     //矩阵快速幂
{
    Matrix ans;
    ans.mt();
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(i==j)
                ans.m[i][j]=1;
            else
                ans.m[i][j]=0;
        }
    }
    while(b)
    {
        if(b&1)
            ans=Mult(ans,a);
        a=Mult(a,a);
        b/=2;
    }
    return ans;
}

int main()
{
    Matrix A;
    A.mt();
    scanf("%lld%lld%lld%lld",&n,&b,&k,&x);
    for (int i=0;i<n;i++)
    {
        int kk;
        scanf ("%d",&kk);
        num[(kk%x)]++;
    }
    for (int i=0;i<x;i++)
        for (int j=0;j<x;j++)
            A.m[i][(i*10+j)%x]+=num[j];
    A=quickpow(A,b);
    printf ("%lld\n",A.m[0][k]);
}


#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
const int mod=1e9+7;
using namespace std;
LL use[101];
int n,b,k,x,a;
struct Matrix
{
    LL m[101][101];
    void mt(){
        memset(m,0,sizeof(m));
    }
};
Matrix Mult(Matrix a, Matrix b)
{
    Matrix c;
    c.mt();
    for(int i = 0; i <x ; i++)
    {
        for(int j = 0; j < x; j++)
        {
            if(a.m[i][j])
            {
                for(int k = 0; k <x; k++)
                {
                    c.m[i][k] += a.m[i][j] * b.m[j][k];
                    c.m[i][k] %= mod;
                }
            }
        }
    }
    return c;
}
Matrix quickpow(Matrix a,int b)
{
    Matrix ans;
    for(int i=0;i<x;i++)
    {
        for(int j=0;j<x;j++)
        {
            if(i==j)
                ans.m[i][j]=1;
            else
                ans.m[i][j]=0;
        }
    }
    while(b)
    {
        if(b&1)
            ans=Mult(ans,a);
        a=Mult(a,a);
        b/=2;
    }
    return ans;
}
int main()
{
    scanf("%d%d%d%d",&n,&b,&k,&x);
    memset(use,0,sizeof(use));
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a);a%=x;
        use[a]++;
    }
    Matrix M,C;
    M.mt(),C.mt();
    for(int i=0;i<=x-1;i++)
    {
        for(int j=0;j<=x-1;j++)
        {
            int c=(i*10+j)%x;
            M.m[c][i]+=use[j];
        }
    }
    for(int i=0;i<=x-1;i++)
        C.m[i][0]+=use[i];
    M=quickpow(M,b-1);
    M=Mult(M,C);
    printf("%lld\n",M.m[k][0]);
    return 0;
}


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

#define LL long long
const int maxn=1e5+5;
const int mod=1e9+7;

LL n,b,k,x;
int num[200];
struct Matrix
{
    LL m[105][105];
    void mt()
    {
        memset(m,0,sizeof(m));
    }
};
Matrix Mult(Matrix a,Matrix b)  //矩阵乘法
{
    Matrix ans;
    for(int i=0;i<105;i++)
    {
        for(int j=0;j<105;j++)
        {
            ans.m[i][j]=0;
            for(int k=0;k<105;k++)
            {
                ans.m[i][j]+=a.m[i][k]*b.m[k][j];
                ans.m[i][j]%=mod;
            }
        }
    }
    return ans;
}
Matrix quickpow(Matrix a,LL b)     //矩阵快速幂
{
    Matrix ans;
    ans.mt();
    for(int i=0;i<105;i++)
    {
        for(int j=0;j<105;j++)
        {
            if(i==j)
                ans.m[i][j]=1;
            else
                ans.m[i][j]=0;
        }
    }
    while(b)
    {
        if(b&1)
            ans=Mult(ans,a);
        a=Mult(a,a);
        b/=2;
    }
    return ans;
}

int main()
{
    Matrix A;
    A.mt();
    scanf("%lld%lld%lld%lld",&n,&b,&k,&x);
    for (int i=0;i<n;i++)
    {
        int kk;
        scanf ("%d",&kk);
        num[(kk%x)]++;
    }
    for (int i=0;i<x;i++)
        for (int j=0;j<x;j++)
            A.m[i][(i*10+j)%x]+=num[j];
    A=quickpow(A,b);
    printf ("%lld\n",A.m[0][k]);
}


#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
const int mod=1e9+7;
using namespace std;
LL use[101];
int n,b,k,x,a;
struct Matrix
{
    LL m[101][101];
    void mt(){
        memset(m,0,sizeof(m));
    }
};
Matrix Mult(Matrix a,Matrix b)
{
    Matrix ans;
    for(int i=0;i<101;i++)
    {
        for(int j=0;j<101;j++)
        {
            ans.m[i][j]=0;
            for(int k=0;k<101;k++)
            {
                ans.m[i][j]+=a.m[i][k]*b.m[k][j];
                ans.m[i][j]%=mod;
            }
        }
    }
    return ans;
}
Matrix quickpow(Matrix a,int b)     //矩阵快速幂
{
    Matrix ans;
    for(int i=0;i<101;i++)
    {
        for(int j=0;j<101;j++)
        {
            if(i==j)
                ans.m[i][j]=1;
            else
                ans.m[i][j]=0;
        }
    }
    while(b)
    {
        if(b&1)
            ans=Mult(ans,a);
        a=Mult(a,a);
        b/=2;
    }
    return ans;
}
void show(Matrix x)
{
    printf("\n-------------------\n");
    for(int i=0;i<=3;i++)
    {
        for(int j=0;j<=3;j++)
        {
            printf("%d ",x.m[i][j]);
        }
        printf("\n");
    }
    printf("\n-------------------\n");
}
int main()
{
    scanf("%d%d%d%d",&n,&b,&k,&x);
    memset(use,0,sizeof(use));
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a);
        a%=x;
        use[a]++;
    }
    Matrix M,C;
    M.mt(),C.mt();
    for(int i=0;i<=x-1;i++)
    {
        for(int j=0;j<=x-1;j++)
        {
            int c=(i*10+j)%x;
            M.m[c][i]+=use[j];
        }
    }
    for(int i=0;i<=x-1;i++)
        C.m[i][0]+=use[i];
    M=quickpow(M,b-1);
    M=Mult(M,C);
    printf("%lld\n",M.m[k][0]);
    return 0;
}
/*
6 5 9 11
8 1 2 1 8 2
*/


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

#define LL long long
const int maxn=1e5+5;
const int mod=1e9+7;

LL n,b,k,x;
int num[200];
struct Matrix
{
    LL m[105][105];
    void mt()
    {
        memset(m,0,sizeof(m));
    }
};
Matrix Mult(Matrix a,Matrix b)  //矩阵乘法
{
    Matrix ans;
    ans.mt();
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            ans.m[i][j]=0;
            for(int k=0;k<n;k++)
            {
                ans.m[i][j]+=a.m[i][k]*b.m[k][j];
                ans.m[i][j]%=mod;
            }
        }
    }
    return ans;
}
Matrix quickpow(Matrix a,int b)     //矩阵快速幂
{
    Matrix ans;
    ans.mt();
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(i==j)
                ans.m[i][j]=1;
            else
                ans.m[i][j]=0;
        }
    }
    while(b)
    {
        if(b&1)
            ans=Mult(ans,a);
        a=Mult(a,a);
        b/=2;
    }
    return ans;
}

int main()
{
    Matrix A;
    A.mt();
    scanf("%lld%lld%lld%lld",&n,&b,&k,&x);
    for (int i=0;i<n;i++)
    {
        int kk;
        scanf ("%d",&kk);
        num[(kk%x)]++;
    }
    for (int i=0;i<x;i++)
        for (int j=0;j<x;j++)
            A.m[i][(i*10+j)%x]+=num[j];
    A=quickpow(A,b);
    printf ("%lld\n",A.m[0][k]);
}


#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
const int mod=1e9+7;
using namespace std;
LL use[101];
int n,b,k,x,a;
struct Matrix
{
    LL m[101][101];
    void mt(){
        memset(m,0,sizeof(m));
    }
};
Matrix Mult(Matrix a, Matrix b)
{
    Matrix c;
    c.mt();
    for(int i = 0; i < 101 ; i++)
    {
        for(int j = 0; j < 101; j++)
        {
            if(a.m[i][j])
            {
                for(int k = 0; k < 101; k++)
                {
                    c.m[i][k] += a.m[i][j] * b.m[j][k];
                    c.m[i][k] %= mod;
                }
            }
        }
    }
    return c;
}
Matrix quickpow(Matrix a,int b)
{
    Matrix ans;
    for(int i=0;i<101;i++)
    {
        for(int j=0;j<101;j++)
        {
            if(i==j)
                ans.m[i][j]=1;
            else
                ans.m[i][j]=0;
        }
    }
    while(b)
    {
        if(b&1)
            ans=Mult(ans,a);
        a=Mult(a,a);
        b/=2;
    }
    return ans;
}
int main()
{
    scanf("%d%d%d%d",&n,&b,&k,&x);
    memset(use,0,sizeof(use));
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a);a%=x;
        use[a]++;
    }
    Matrix M,C;
    M.mt(),C.mt();
    for(int i=0;i<=x-1;i++)
    {
        for(int j=0;j<=x-1;j++)
        {
            int c=(i*10+j)%x;
            M.m[c][i]+=use[j];
        }
    }
    for(int i=0;i<=x-1;i++)
        C.m[i][0]+=use[i];
    M=quickpow(M,b-1);
    M=Mult(M,C);
    printf("%lld\n",M.m[k][0]);
    return 0;
}


// In the name of ALLAH
#include <bits/stdc++.h>
using namespace std;

#define PB push_back
#define PF push_front
#define MP make_pair
#define FF first
#define SS second
#define _sz(x) (int)x.size()

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;

int const N = 100 + 20, MX = 10 + 2, mod = 1e9 + 7;
int n, b, k, x, cnt[MX];
int mat[N][N];

inline void add (int &a, int b){
	a += b;
	if (a >= mod) a -= mod;

inline void mul (int a[N][N], int b[N][N]){
	int res[N][N]; memset(res, 0, sizeof res);
  	for (int k = 0; k < x; k ++)
	  	for (int i = 0; i < x; i ++)
		  	for (int j = 0; j < x; j ++)
				add(res[i][j], a[i][k] * 1LL * b[k][j] % mod);

	memcpy(a, res, sizeof res);
}

inline void bpow (int mat[N][N], int y){
	int res[N][N]; memset(res, 0, sizeof res);
	for (int i = 0; i < x; i ++)
	  	for (int j = 0; j < x; j ++)
			res[i][j] = bool(i == j);

  	while (y){
		if (y & 1) mul(res, mat);
		mul(mat, mat);
		y >>= 1;
	}

	memcpy(mat, res, sizeof res);
}

int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> b >> k >> x;
	for (int i = 0; i < n; i ++){
		int y; cin >> y; cnt[y % x] ++;
	}

	for (int i = 0; i < x; i ++){
		for (int j = 0; j < x; j ++){
			int y = (i - 10 * j) % x;
			if (y < 0) y += x;

			mat[j][i] = cnt[y];
		}
	}

	bpow(mat, b - 1);

	int res = 0;

	for (int j = 0; j < x; j ++)
	  	add(res, cnt[j] * 1LL * mat[j][k] % mod);

	cout << res << endl;
}


// In the name of ALLAH
#include <bits/stdc++.h>
using namespace std;

#define PB push_back
#define PF push_front
#define MP make_pair
#define FF first
#define SS second
#define _sz(x) (int)x.size()

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;

int const N = 100 + 20, mod = 1e9 + 7;
int n, b, k, x, cnt[N];
int mat[N][N];

inline void add (int &a, int b){
	a += b;
	if (a >= mod) a -= mod;
}

inline void mul (int a[N][N], int b[N][N]){
	int res[N][N]; memset(res, 0, sizeof res);
  	for (int k = 0; k < x; k ++)
	  	for (int i = 0; i < x; i ++)
		  	for (int j = 0; j < x; j ++)
				add(res[i][j], a[i][k] * 1LL * b[k][j] % mod);

	memcpy(a, res, sizeof res);
}

inline void bpow (int mat[N][N], int y){
	int res[N][N]; memset(res, 0, sizeof res);
	for (int i = 0; i < x; i ++)
	  	for (int j = 0; j < x; j ++)
			res[i][j] = bool(i == j);

  	while (y){
		if (y & 1) mul(res, mat);
		mul(mat, mat);
		y >>= 1;
	}

	memcpy(mat, res, sizeof res);
}

int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> b >> k >> x;
	for (int i = 0; i < n; i ++){
		int y; cin >> y; cnt[y % x] ++;
	}

	for (int i = 0; i < x; i ++){
		for (int j = 0; j < x; j ++){
			int y = (i - 10 * j) % x;
			if (y < 0) y += x;

			mat[j][i] = cnt[y];
		}
	}

	bpow(mat, b - 1);

	int res = 0;

	for (int j = 0; j < x; j ++)
	  	add(res, cnt[j] * 1LL * mat[j][k] % mod);

	cout << res << endl;
}


// In the name of ALLAH
#include <bits/stdc++.h>
using namespace std;

#define PB push_back
#define PF push_front
#define MP make_pair
#define FF first
#define SS second
#define _sz(x) (int)x.size()

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;

int const N = 100 + 20, mod = 1e9 + 7;
int n, b, k, x, cnt[N];
int mat[N][N];

inline void add (int &a, int b){
	a += b;
	if (a >= mod) a -= mod;
}

inline void mul (int a[N][N], int b[N][N]){
	int res[N][N]; memset(res, 0, sizeof res);
  	for (int k = 0; k < x; k ++)
	  	for (int i = 0; i < x; i ++)
		  	for (int j = 0; j < x; j ++)
				add(res[i][j], a[i][k] * 1LL * b[k][j] % mod);

	memcpy(a, res, sizeof res);
}

inline void bpow (int mat[N][N], int y){
	int res[N][N]; memset(res, 0, sizeof res);
	for (int i = 0; i < x; i ++)
	  	for (int j = 0; j < x; j ++)
			res[i][j] = bool(i == j);

  	while (y){
		if (y & 1) mul(res, mat);
		mul(mat, mat);
		y >>= 1;
	}

	memcpy(mat, res, sizeof res);
}

int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> b >> k >> x;
	for (int i = 0; i < n; i ++){
		int y; cin >> y; cnt[y % x] ++;
	}

	for (int i = 0; i < x; i ++){
		for (int j = 0; j < x; j ++){
			int y = (i - 10 * j) % x;
			if (y < 0) y += x;

			mat[j][i] = cnt[y];
		}
	}

	bpow(mat, b - 1);

	int res = 0;

	for (int j = 0; j < x; j ++){
	  	add(res, cnt[j] * 1LL * mat[j][k] % mod);

	cout << res << endl;
}


// In the name of ALLAH
#include <bits/stdc++.h>
using namespace std;

#define PB push_back
#define PF push_front
#define MP make_pair
#define FF first
#define SS second
#define _sz(x) (int)x.size()

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;

int const N = 100 + 20, MX = 10 + 2, mod = 1e9 + 7;
int n, b, k, x, cnt[MX];
int mat[N][N];

inline void add (int &a, int b){
	a += b;
	if (a >= mod) a -= mod;
}

inline void mul (int a[N][N], int b[N][N]){
	int res[N][N]; memset(res, 0, sizeof res);
  	for (int k = 0; k < x; k ++)
	  	for (int i = 0; i < x; i ++)
		  	for (int j = 0; j < x; j ++)
				add(res[i][j], a[i][k] * 1LL * b[k][j] % mod);

	memcpy(a, res, sizeof res);
}

inline void bpow (int mat[N][N], int y){
	int res[N][N]; memset(res, 0, sizeof res);
	for (int i = 0; i < x; i ++)
	  	for (int j = 0; j < x; j ++)
			res[i][j] = bool(i == j);

  	while (y){
		if (y & 1) mul(res, mat);
		mul(mat, mat);
		y >>= 1;
	}

	memcpy(mat, res, sizeof res);
}

int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> b >> k >> x;
	for (int i = 0; i < n; i ++){
		int y; cin >> y; cnt[y % x] ++;
	}

	for (int i = 0; i < x; i ++){
		for (int j = 0; j < x; j ++){
			int y = (i - 10 * j) % x;
			if (y < 0) y += x;

			mat[j][i] = cnt[y];
		}
	}

	bpow(mat, b - 1);

	int res = 0;

	for (int j = 0; j < x; j ++)
	  	add(res, cnt[j] * 1LL * mat[j][k] % mod);

	cout << res << endl;
//

}


// In the name of ALLAH
#include <bits/stdc++.h>
using namespace std;

#define PB push_back
#define PF push_front
#define MP make_pair
#define FF first
#define SS second
#define _sz(x) (int)x.size()

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;

int const N = 100 + 20, MX = 10 + 2, mod = 1e9 + 7;
int n, b, k, x, cnt[MX];
int mat[N][N];

inline void add (int &a, int b){
	a += b;
	if (a >= mod) a -= mod;
}

inline void mul (int a[N][N], int b[N][N]){
	int res[N][N]; memset(res, 0, sizeof res);
  	for (int k = 0; k < x; k ++)
	  	for (int i = 0; i < x; i ++)
		  	for (int j = 0; j < x; j ++)
				add(res[i][j], a[i][k] * 1LL * b[k][j] % mod);

	memcpy(a, res, sizeof res);
}

inline void bpow (int mat[N][N], int y){
	int res[N][N]; memset(res, 0, sizeof res);
	for (int i = 0; i < x; i ++)
	  	for (int j = 0; j < x; j ++)
			res[i][j] = bool(i == j);

  	while (y){
		if (y & 1) mul(res, mat);
		mul(mat, mat);
		y >>= 1;
	}

	memcpy(mat, res, sizeof res);
}

int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> b >> k >> x;
	for (int i = 0; i < n; i ++){
		int y; cin >> y; cnt[y % x] ++;
	}

	for (int i = 0; i < x; i ++){
		for (int j = 0; j < x; j ++){
			int y = (i - 10 * j) % x;
			if (y < 0) y += x;

			mat[j][i] = cnt[y];
		}
	}

	bpow(mat, b - 1);

	int res = 0;

	for (int j = 0; j < x; j ++)
	  	add(res, cnt[j] * 1LL * mat[j][k] % mod);

	cout << res << endl;
}


// In the name of ALLAH
#include <bits/stdc++.h>
using namespace std;

#define PB push_back
#define PF push_front
#define MP make_pair
#define FF first
#define SS second
#define _sz(x) (int)x.size()

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;

int const N = 100 + 20, MX = 10 + 2, mod = 1e9 + 7;
int n, b, k, x, cnt[MX];
int mat[N][N];

inline void add (int &a, int b){
	a += b;
	if (a >= mod) a -= mod;
}

inline void mul (int a[N][N], int b[N][N]){
	int res[N][N]; memset(res, 0, sizeof res);
  	for (int k = 0; k < x; k ++)
	  	for (int i = 0; i < x; i ++)
		  	for (int j = 0; j < x; j ++)
				add(res[i][j], a[i][k] * 1LL * b[k][j] % mod);

	memcpy(a, res, sizeof res);
}

inline void bpow (int mat[N][N], int y){
	int res[N][N]; memset(res, 0, sizeof res);
	for (int i = 0; i < x; i ++)
	  	for (int j = 0; j < x; j ++)
			res[i][j] = bool(i == j);

  	while (y){
		if (y & 1) mul(res, mat);
		mul(mat, mat);
		y >>= 1;
	}

	memcpy(mat, res, sizeof res);
}

int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> b >> k >> x;
	for (int i = 0; i < n; i ++){
		int y; cin >> y; cnt[y % x] ++;
	}

	for (int i = 0; i < x; i ++){
		for (int j = 0; j < x; j ++){
			int y = (i - 10 * j) % x;
			if (y < 0) y += x;

			mat[j][i] = cnt[y];
		}
	}

	bpow(mat, b - 1);

	int res = 0;

	for (int j = 0; j < MX; j ++)
	  	add(res, cnt[j] * 1LL * mat[j][k] % mod);

	cout << res << endl;
}


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

// why am I so weak

const long long MOD = 1e9 + 7;

typedef vector<long long> vec;
typedef vector<vec> mat;

int n, b, k, x;
int cnt[10];
int cool[155];

mat mul(mat &A, mat &B) {
	mat C(A.size(), vec(B[0].size()));

	for (int i = 0; i < (int)A.size(); i++) {
		for (int k = 0; k < (int)B.size(); k++) {
			for (int j = 0; j < (int)B[0].size(); j++) {
				C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
			}
		}
	}

	return C;
}
mat pow(mat A, int k) {
	mat B(A.size(), vec(A[0].size()));

	for (int i = 0; i < x; i++) B[i][i] = 1ll;

	while (k > 0) {
		if (k & 1) B = mul(B, A);
		A = mul(A, A);
		k >>= 1;
	}

	return B;
}
int main() {
	// dp[(j * 10 + k) % x] = dp[j] * numof(k)

	scanf("%d %d %d %d", &n, &b, &k, &x);

	for (int i = 0; i < n; i++) {
		int y;

		scanf("%d", &y);
		cnt[y]++;
		cool[y % x]++;
	}

	mat A(x, vec(x));

	for (int i = 0; i < x; i++) for (int j = 1; j < 10; j++) {
		A[(i * 10 + j) % x][i] += cnt[j];
	}

	A = pow(A, b - 1);

	long long res = 0ll;

	for (int i = 0; i < x; i++) {
		res = (res + A[k][i] * cool[i]) % MOD;
	}

	printf("%lld\n", res);

	return 0;
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <deque>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <climits>
#include <cctype>
#include <utility>
using namespace std;

#define ft first
#define sd second
#define pb push_back
#define endl '\n'
#define buli(x) __builtin_popcountll(x)
#define cpy(a,e) memcpy(a,e,sizeof(e))
#define clr(a,e) memset(a,e,sizeof(a))
#define iter(c) __typeof((c).begin())
#define tr(c,i) for (iter(c) i=(c).begin();i!=(c).end();i++)
#define rep(i,n) for (int (i)=0;(i)<(n);i++)
#define repd(i,n) for (int (i)=(n)-1;(i)>=0;i--)
#define reps(i,s,e) for (int (i)=(s);(i)<=(e);i++)
#define repds(i,s,e) for (int (i)=(s);(i)>=(e);i--)
#define repl(i,s,e) for (int (i)=(s);(i);i=e[i])

const long long MOD=1e9+7;

int n,b,k,x;
int c[15];
map<long long,long long> dp;
map<long long,bool> used;

long long big_mod(long long ind) {
	if (ind==0) return 1;
	if (ind==1) return 1LL*n;
	if (used[ind]) return dp[ind];
	long long res=(big_mod(ind/2)%MOD*big_mod(ind-ind/2)%MOD)%MOD;
	used[ind]=1;
	return dp[ind]=res;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> b >> k >> x;
	int y;
	rep(i,n) {
		cin >> y;
		y%=x;
		c[y]++;
	}
	long long res=0LL;
	if (k<10) {
		res=1LL*c[k];
		res*=1LL*big_mod(b-1);
	} else if (k<100) {
		int wtf=k%10;
		res=1LL*c[wtf];
		if (b==1) {
			res=0;
			cout << res << endl;
			return 0;
		}
		wtf=(k/10)%10;
		res*=1LL*c[wtf];
		res*=big_mod(b-2);
	}
	cout << res << endl;
	return 0;
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <deque>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <climits>
#include <cctype>
#include <utility>
using namespace std;

#define ft first
#define sd second
#define pb push_back
#define endl '\n'
#define buli(x) __builtin_popcountll(x)
#define cpy(a,e) memcpy(a,e,sizeof(e))
#define clr(a,e) memset(a,e,sizeof(a))
#define iter(c) __typeof((c).begin())
#define tr(c,i) for (iter(c) i=(c).begin();i!=(c).end();i++)
#define rep(i,n) for (int (i)=0;(i)<(n);i++)
#define repd(i,n) for (int (i)=(n)-1;(i)>=0;i--)
#define reps(i,s,e) for (int (i)=(s);(i)<=(e);i++)
#define repds(i,s,e) for (int (i)=(s);(i)>=(e);i--)
#define repl(i,s,e) for (int (i)=(s);(i);i=e[i])

const long long MOD=1e9+7;

int n,b,k,x;
int c[15];
map<long long,long long> dp;
map<long long,bool> used;

long long big_mod(long long ind) {
	if (ind==0) return 1;
	if (ind==1) return 1LL*n;
	if (used[ind]) return dp[ind];
	long long res=(big_mod(ind/2)%MOD*big_mod(ind-ind/2)%MOD)%MOD;
	used[ind]=1;
	return dp[ind]=res;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> b >> k >> x;
	int y;
	rep(i,n) {
		cin >> y;
		y%=x;
		c[y]++;
	}
	long long res=0LL;
	if (k<10) {
		res=1LL*c[k];
		res*=1LL*big_mod(b-1);
	} else if (k<100) {
		if (b==1) {
			res=0;
			cout << res << endl;
			return 0;
		}
		reps(i,1,9) {
			if (!c[i]) continue;
			reps(j,1,9) {
				if (!c[j]) continue;
				if ((i*10+j)%x==k) {
					res+=1LL*c[i]*c[j];
				}
			}
		}
		res*=big_mod(b-2);
	}
	cout << res << endl;
	return 0;
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <deque>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <climits>
#include <cctype>
#include <utility>
using namespace std;

#define ft first
#define sd second
#define pb push_back
#define endl '\n'
#define buli(x) __builtin_popcountll(x)
#define cpy(a,e) memcpy(a,e,sizeof(e))
#define clr(a,e) memset(a,e,sizeof(a))
#define iter(c) __typeof((c).begin())
#define tr(c,i) for (iter(c) i=(c).begin();i!=(c).end();i++)
#define rep(i,n) for (int (i)=0;(i)<(n);i++)
#define repd(i,n) for (int (i)=(n)-1;(i)>=0;i--)
#define reps(i,s,e) for (int (i)=(s);(i)<=(e);i++)
#define repds(i,s,e) for (int (i)=(s);(i)>=(e);i--)
#define repl(i,s,e) for (int (i)=(s);(i);i=e[i])

const long long MOD=1e9+7;

int n,b,k,x;
int c[15];
map<long long,long long> dp;
map<long long,bool> used;

long long big_mod(long long ind) {
	if (ind==0) return 1;
	if (ind==1) return 1LL*n;
	if (used[ind]) return dp[ind];
	long long res=(big_mod(ind/2)%MOD*big_mod(ind-ind/2)%MOD)%MOD;
	used[ind]=1;
	return dp[ind]=res;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> b >> k >> x;
	int y;
	rep(i,n) {
		cin >> y;
		c[y]++;
	}
	long long res=0LL;
	if (k<10) {
		reps(i,1,9) {
			if (!c[i]) continue;
			if (i%x==k) {
				res+=c[i];
			}
		}
		res*=1LL*big_mod(b-1);
	} else if (k<100) {
		if (b==1) {
			res=0;
			cout << res << endl;
			return 0;
		}
		reps(i,1,9) {
			if (!c[i]) continue;
			reps(j,1,9) {
				if (!c[j]) continue;
				if ((i*10+j)%x==k) {
					res+=1LL*c[i]*c[j];
				}
			}
		}
		res*=big_mod(b-2);
	}
	cout << res << endl;
	return 0;
}


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

// why am I so weak

const long long MOD = 1e9 + 7;

typedef vector<long long> vec;
typedef vector<vec> mat;

int n, b, k, x;
int cnt[10];
int cool[155];

mat mul(mat &A, mat &B) {
	mat C(A.size(), vec(B[0].size()));

	for (int i = 0; i < (int)A.size(); i++) {
		for (int k = 0; k < (int)B.size(); k++) {
			for (int j = 0; j < (int)B[0].size(); j++) {
				C[i][j] = (C[i][j] + A[i][k] * B[i][k]) % MOD;
			}
		}
	}

	return C;
}
mat pow(mat A, int k) {
	mat B(A.size(), vec(A[0].size()));

	for (int i = 0; i < x; i++) B[i][i] = 1ll;

	while (k > 0) {
		if (k & 1) B = mul(B, A);
		A = mul(A, A);
		k >>= 1;
	}

	return B;
}
int main() {
	// dp[(j * 10 + k) % x] = dp[j] * numof(k)

	scanf("%d %d %d %d", &n, &b, &k, &x);

	for (int i = 0; i < n; i++) {
		int y;

		scanf("%d", &y);
		cnt[y]++;
		cool[y % x]++;
	}

	mat A(x, vec(x));

	for (int i = 0; i < x; i++) for (int j = 1; j < 10; j++) {
		A[(i * 10 + j) % x][i] += cnt[j];
	}

	A = pow(A, b - 1);

	long long res = 0ll;

	for (int i = 0; i < x; i++) {
		res = (res + A[k][i] * cool[i]) % MOD;
	}

	printf("%lld\n", res);

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef int mat[200][200];
typedef long long ll;

const ll N = 1e2 + 10, mod = 1e9 + 7;
ll n, b, k, x, mark[N + 10];
mat base, a1, ans;

void mul(mat a, mat b, mat c) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
				c[i][j] = 0;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			for (ll k = 0; k < N; k++)
				c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
}

void swp(mat a, mat b) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			swap(a[i][j], b[i][j]);
}

void power(ll k, mat a) {
	mat b, c;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++) {
				if (i == j)
					b[i][j] = 1;
				else
					b[i][j] = 0;
			}
	for (; k; k /= 2) {
		if (k % 2) {
			mul(a, b, c);
			swp(b, c);
		}
		mul(a, a, c);
		swp(a, c);
	}
	swp(a, b);
}

int main() {
	cin >> n >> b >> k >> x;
	for (ll i = 0; i < n; i++) {
		ll a;
		cin >> a;
		a %= x;
		mark[a]++;
	}
	if (b == 1)
		return cout << mark[k], 0;
	for (ll i = 0, u = x - 1; i < x; i++, u--) {
		for (int j = 0, l = x - 1; j < x; j++, l--) {
			for (int k = 0; k < x; k++) {
				if ((10 * l + k) % x == u)
					base[i][j] += mark[k];
			}
		}
	}
	if (n == 50000)
		for (int i = 0; i < x; i++) {
			for (int j = 0; j < x; j++)
				cout << base[i][j] << " ";
			cout << endl;
		}
	for (ll i = 0, k = x - 1; i < x; i++, k--)
		a1[i][0] = mark[i];
	power(b - 1, base);
	mul(base, a1, ans);
	cout << ans[x - (k + 1)][0];
}


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

const ll N = 1e2 + 10, mod = 1e9 + 7;
ll n, b, k, x, mark[N + 10];
mat base, a1, ans;

void mul(mat a, mat b, mat c) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
				c[i][j] = 0;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			for (ll k = 0; k < N; k++)
				c[i][j] = (c[i][j] + (a[i][k] * b[k][j]) % mod) % mod;
}

void swp(mat a, mat b) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			swap(a[i][j], b[i][j]);
}

void power(ll k, mat a) {
	mat b, c;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++) {
				if (i == j)
					b[i][j] = 1;
				else
					b[i][j] = 0;
			}
	for (; k; k /= 2) {
		if (k % 2) {
			mul(a, b, c);
			swp(b, c);
		}
		mul(a, a, c);
		swp(a, c);
	}
	swp(a, b);
}

int main() {
	cin >> n >> b >> k >> x;
	for (ll i = 0; i < n; i++) {
		ll a;
		cin >> a;
		a %= x;
		mark[a]++;
	}
	if (b == 1)
		return cout << mark[k], 0;
	for (ll i = 0, u = x - 1; i < x; i++, u--) {
		for (ll j = 0, l = x - 1; j < x; j++, l--) {
			for (ll k = 0; k < x; k++) {
				if ((10 * l + k) % x == u)
					base[i][j] += mark[k];
			}
		}
	}
	for (ll i = 0, k = x - 1; i < x; i++, k--)
		a1[i][0] = mark[i];
	power(b - 1, base);
	mul(base, a1, ans);
	cout << ans[x - (k + 1)][0];
}


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

const ll N = 1e2, mod = 1e9 + 7;
ll n, b, k, x, mark[N + 10];
mat base, a1, ans;

void mul(mat a, mat b, mat c) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
				c[i][j] = 0;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			for (ll k = 0; k < N; k++)
				c[i][j] = (c[i][j] + (a[i][k] * b[k][j]) % mod) % mod;
}

void swp(mat a, mat b) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			swap(a[i][j], b[i][j]);
}

void power(ll k, mat a) {
	mat b, c;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++) {
				if (i == j)
					b[i][j] = 1;
				else
					b[i][j] = 0;
			}
	for (; k; k /= 2) {
		if (k % 2) {
			mul(a, b, c);
			swp(b, c);
		}
		mul(a, a, c);
		swp(a, c);
	}
	swp(a, b);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n >> b >> k >> x;
	for (ll i = 0; i < n; i++) {
		ll a;
		cin >> a;
		a %= x;
		mark[a]++;
	}
	if (b == 1)
		return cout << mark[k], 0;
	for (ll i = 0, u = x - 1; i < x; i++, u--) {
		for (ll j = 0, l = x - 1; j < x; j++, l--) {
			for (ll k = 0; k < x; k++) {
				if ((10 * l + k) % x == u)
					base[i][j] = (base[i][j] + mark[k]) % mod;
			}
		}
	}
	for (ll i = 0, k = x - 1; i < x; i++, k--)
		a1[i][0] = mark[k];
	power(b - 1, base);
	mul(base, a1, ans);
	cout << ans[x - (k + 1)][0];
}


#include <bits/stdc++.h>
using namespace std;
typedef int mat[200][200];
typedef long long ll;

const ll N = 1e2 + 10, mod = 1e9 + 7;
ll n, b, k, x, mark[N + 10];
mat base, a1, ans;

void mul(mat a, mat b, mat c) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
				c[i][j] = 0;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			for (ll k = 0; k < N; k++)
				c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
}

void swp(mat a, mat b) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			swap(a[i][j], b[i][j]);
}

void power(ll k, mat a) {
	mat b, c;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++) {
				if (i == j)
					b[i][j] = 1;
				else
					b[i][j] = 0;
			}
	for (; k; k /= 2) {
		if (k % 2) {
			mul(a, b, c);
			swp(b, c);
		}
		mul(a, a, c);
		swp(a, c);
	}
	swp(a, b);
}

int main() {
	cin >> n >> b >> k >> x;
	for (ll i = 0; i < n; i++) {
		ll a;
		cin >> a;
		a %= x;
		mark[a]++;
	}
	if (b == 1)
		return cout << mark[k], 0;
	for (ll i = 0, u = x - 1; i < x; i++, u--) {
		for (int j = 0, l = x - 1; j < x; j++, l--) {
			for (int k = 0; k < x; k++) {
				if ((10 * l + k) % x == u)
					base[i][j] += mark[k];
			}
		}
	}
	for (int i = 0; i < x; i++) {
		for (int j = 0; j < x; j++)
			cout << base[i][j] << " ";
		cout << endl;
	}
	for (ll i = 0, k = x - 1; i < x; i++, k--)
		a1[i][0] = mark[i];
	power(b - 1, base);
	mul(base, a1, ans);
	cout << ans[x - (k + 1)][0];
}


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

const ll N = 1e2 + 10, mod = 1e9 + 7;
ll n, b, k, x, mark[N + 10];
mat base, a1, ans;

void mul(mat a, mat b, mat c) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
				c[i][j] = 0;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			for (ll k = 0; k < N; k++)
				c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
}

void swp(mat a, mat b) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			swap(a[i][j], b[i][j]);
}

void power(ll k, mat a) {
	mat b, c;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++) {
				if (i == j)
					b[i][j] = 1;
				else
					b[i][j] = 0;
			}
	for (; k; k /= 2) {
		if (k % 2) {
			mul(a, b, c);
			swp(b, c);
		}
		mul(a, a, c);
		swp(a, c);
	}
	swp(a, b);
}

int main() {
	cin >> n >> b >> k >> x;
	for (ll i = 0; i < n; i++) {
		ll a;
		cin >> a;
		a %= x;
		mark[a]++;
	}
	if (b == 1)
		return cout << mark[k], 0;
	for (ll i = 0, u = x - 1; i < x; i++, u--) {
		for (ll j = 0, l = x - 1; j < x; j++, l--) {
			for (ll k = 0; k < x; k++) {
				if ((10 * l + k) % x == u)
					base[i][j] += mark[k];
			}
		}
	}
	for (ll i = 0, k = x - 1; i < x; i++, k--)
		a1[i][0] = mark[i];
	power(b - 1, base);
	mul(base, a1, ans);
	cout << ans[x - (k + 1)][0];
}


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

const ll N = 1e2, mod = 1e9 + 7;
ll n, b, k, x, mark[N + 10];
mat base, a1, ans;

void mul(mat a, mat b, mat c) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
				c[i][j] = 0;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			for (ll k = 0; k < N; k++)
				c[i][j] = (c[i][j] + (a[i][k] * b[k][j]) % mod) % mod;
}

void swp(mat a, mat b) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			swap(a[i][j], b[i][j]);
}

void power(ll k, mat a) {
	mat b, c;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++) {
				if (i == j)
					b[i][j] = 1;
				else
					b[i][j] = 0;
			}
	for (; k; k /= 2) {
		if (k % 2) {
			mul(a, b, c);
			swp(b, c);
		}
		mul(a, a, c);
		swp(a, c);
	}
	swp(a, b);
}

int main() {
	cin >> n >> b >> k >> x;
	for (ll i = 0; i < n; i++) {
		ll a;
		cin >> a;
		a %= x;
		mark[a]++;
	}
	if (b == 1)
		return cout << mark[k], 0;
	for (ll i = 0, u = x - 1; i < x; i++, u--) {
		for (ll j = 0, l = x - 1; j < x; j++, l--) {
			for (ll k = 0; k < x; k++) {
				if ((10 * l + k) % x == u)
					base[i][j] = (base + mark[k]) % mod;
			}
		}
	}
	for (ll i = 0, k = x - 1; i < x; i++, k--)
		a1[i][0] = mark[i];
	power(b - 1, base);
	mul(base, a1, ans);
	cout << ans[x - (k + 1)][0];
}


#include <bits/stdc++.h>
using namespace std;
typedef int mat[200][200];
typedef long long ll;

const ll N = 1e2 + 10, mod = 1e9 + 7;
ll n, b, k, x, mark[N + 10];
mat base, a1, ans;

void mul(mat a, mat b, mat c) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
				c[i][j] = 0;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			for (ll k = 0; k < N; k++)
				c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
}

void swp(mat a, mat b) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			swap(a[i][j], b[i][j]);
}

void power(ll k, mat a) {
	mat b, c;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++) {
				if (i == j)
					b[i][j] = 1;
				else
					b[i][j] = 0;
			}
	for (; k; k /= 2) {
		if (k % 2) {
			mul(a, b, c);
			swp(b, c);
		}
		mul(a, a, c);
		swp(a, c);
	}
	swp(a, b);
}

ll div(ll k) {
	ll res = 0;
	for (ll i = 0; i < x; i++)
		if ((i * 10) % x == k)
			res = (res + mark[i]) % mod;
	return res;
}

int main() {
	cin >> n >> b >> k >> x;
	for (ll i = 0; i < n; i++) {
		ll a;
		cin >> a;
		a %= x;
		mark[a]++;
	}
	if (b == 1)
		return cout << mark[k], 0;
//	for (ll i = 0; i < x; i++)
//		cout << "mark : " << i << " " << mark[i] << endl;
	for (ll i = 0, u = 0; i < x; i++, u--) {
		ll tmp = 0;
		if (u < 0)
			u += x;
		u %= x;
		for (ll j = u; j < x; j++, tmp++)
			base[i][tmp] = mark[j];
		for (ll j = 0; j < u; j++, tmp++)
			base[i][tmp] = mark[j];
	}

	for (ll i = 0, k = x - 1; i < x; i++, k--)
		a1[i][0] = div(k);//, cout << "salam : " << k << " " << a1[i][0] << endl;
	power(b - 1, base);
	mul(base, a1, ans);
//	for (ll i = 0; i < x; i++) {
//		cout << "a1 : " << a1[i][0] << endl;
//	}
//	for (ll i = 0; i < x; i++) {
//		for (ll j = 0; j < x; j++)
//			cout << base[i][j] << " ";
//		cout << endl;
//	}
//	for (ll i = 0; i < x; i++)
//		cout << "ans : " << (x - i - 1) << " " << ans[i][0] << endl;
	cout << ans[x - (k + 1)][0];
}


#include <bits/stdc++.h>
using namespace std;
typedef int mat[200][200];
typedef long long ll;

const ll N = 1e2 + 10, mod = 1e9 + 7;
ll n, b, k, x, mark[N + 10];
mat base, a1, ans;

void mul(mat a, mat b, mat c) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
				c[i][j] = 0;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			for (ll k = 0; k < N; k++)
				c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
}

void swp(mat a, mat b) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			swap(a[i][j], b[i][j]);
}

void power(ll k, mat a) {
	mat b, c;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++) {
				if (i == j)
					b[i][j] = 1;
				else
					b[i][j] = 0;
			}
	for (; k; k /= 2) {
		if (k % 2) {
			mul(a, b, c);
			swp(b, c);
		}
		mul(a, a, c);
		swp(a, c);
	}
	swp(a, b);
}

ll div(ll k) {
	ll res = 0;
	for (ll i = 0; i < x; i++)
		if ((i * 10) % x == k)
			res = (res + mark[i]) % mod;
	return res;
}

int main() {
	cin >> n >> b >> k >> x;
	for (ll i = 0; i < n; i++) {
		ll a;
		cin >> a;
		a %= x;
		mark[a]++;
	}
	if (b == 1)
		return cout << mark[k], 0;
//	for (ll i = 0; i < x; i++)
//		cout << "mark : " << i << " " << mark[i] << endl;
	for (ll i = 0, u = x - 1; i < x; i++, u--) {
		for (int j = 0, l = x - 1; j < x; j++, l--) {
			for (int k = 0; k < x; k++) {
				if ((10 * l + k) % x == u)
					base[i][j] = mark[k];
			}
		}
	}

	for (ll i = 0, k = x - 1; i < x; i++, k--)
		a1[i][0] = mark[i];//, cout << "salam : " << k << " " << a1[i][0] << endl;
	power(b - 1, base);
	mul(base, a1, ans);
//	for (ll i = 0; i < x; i++) {
//		cout << "a1 : " << a1[i][0] << endl;
//	}
//	for (ll i = 0; i < x; i++) {
//		for (ll j = 0; j < x; j++)
//			cout << base[i][j] << " ";
//		cout << endl;
//	}
//	for (ll i = 0; i < x; i++)
//		cout << "ans : " << (x - i - 1) << " " << ans[i][0] << endl;
	cout << ans[x - (k + 1)][0];
}


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

const ll N = 2e2, mod = 1e9 + 7;
ll n, b, k, x, mark[N + 10];
mat base, a1, ans;

void mul(mat a, mat b, mat c) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
				c[i][j] = 0;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			for (ll k = 0; k < N; k++)
				c[i][j] = (c[i][j] + (a[i][k] * b[k][j]) % mod) % mod;
}

void swp(mat a, mat b) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			swap(a[i][j], b[i][j]);
}

void power(ll k, mat a) {
	mat b, c;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++) {
				if (i == j)
					b[i][j] = 1;
				else
					b[i][j] = 0;
			}
	for (; k; k /= 2) {
		if (k % 2) {
			mul(a, b, c);
			swp(b, c);
		}
		mul(a, a, c);
		swp(a, c);
	}
	swp(a, b);
}

int main() {
	cin >> n >> b >> k >> x;
	for (ll i = 0; i < n; i++) {
		ll a;
		cin >> a;
		a %= x;
		mark[a]++;
	}
	if (b == 1)
		return cout << mark[k], 0;
	for (ll i = 0, u = x - 1; i < x; i++, u--) {
		for (ll j = 0, l = x - 1; j < x; j++, l--) {
			for (ll k = 0; k < x; k++) {
				if ((10 * l + k) % x == u)
					base[i][j] += mark[k];
			}
		}
	}
	for (ll i = 0, k = x - 1; i < x; i++, k--)
		a1[i][0] = mark[i];
	power(b - 1, base);
	mul(base, a1, ans);
	cout << ans[x - (k + 1)][0];
}


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

const ll N = 1e2, mod = 1e9 + 7;
ll n, b, k, x, mark[N + 10];
mat base, a1, ans;

void mul(mat a, mat b, mat c) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
				c[i][j] = 0;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			for (ll k = 0; k < N; k++)
				c[i][j] = (c[i][j] + (a[i][k] * b[k][j]) % mod) % mod;
}

void swp(mat a, mat b) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			swap(a[i][j], b[i][j]);
}

void power(ll k, mat a) {
	mat b, c;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++) {
				if (i == j)
					b[i][j] = 1;
				else
					b[i][j] = 0;
			}
	for (; k; k /= 2) {
		if (k % 2) {
			mul(a, b, c);
			swp(b, c);
		}
		mul(a, a, c);
		swp(a, c);
	}
	swp(a, b);
}

int main() {
	cin >> n >> b >> k >> x;
	for (ll i = 0; i < n; i++) {
		ll a;
		cin >> a;
		a %= x;
		mark[a]++;
	}
	if (b == 1)
		return cout << mark[k], 0;
	for (ll i = 0, u = x - 1; i < x; i++, u--) {
		for (ll j = 0, l = x - 1; j < x; j++, l--) {
			for (ll k = 0; k < x; k++) {
				if ((10 * l + k) % x == u)
					base[i][j] = (base[i][j] + mark[k]) % mod;
			}
		}
	}
	for (ll i = 0, k = x - 1; i < x; i++, k--)
		a1[i][0] = mark[k];
	power(b - 1, base);
	mul(base, a1, ans);
	cout << ans[x - (k + 1)][0];
}


#include <bits/stdc++.h>
using namespace std;
typedef int mat[200][200];
typedef long long ll;

const ll N = 1e2 + 10, mod = 1e9 + 7;
ll n, b, k, x, mark[N + 10];
mat base, a1, ans;

void mul(mat a, mat b, mat c) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
				c[i][j] = 0;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			for (ll k = 0; k < N; k++)
				c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
}

void swp(mat a, mat b) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			swap(a[i][j], b[i][j]);
}

void power(ll k, mat a) {
	mat b, c;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++) {
				if (i == j)
					b[i][j] = 1;
				else
					b[i][j] = 0;
			}
	for (; k; k /= 2) {
		if (k % 2) {
			mul(a, b, c);
			swp(b, c);
		}
		mul(a, a, c);
		swp(a, c);
	}
	swp(a, b);
}

int main() {
	cin >> n >> b >> k >> x;
	for (ll i = 0; i < n; i++) {
		ll a;
		cin >> a;
		a %= x;
		mark[a]++;
	}
	if (b == 1)
		return cout << mark[k], 0;
	for (ll i = 0, u = x - 1; i < x; i++, u--) {
		for (int j = 0, l = x - 1; j < x; j++, l--) {
			for (int k = 0; k < x; k++) {
				if ((10 * l + k) % x == u)
					base[i][j] += mark[k];
			}
		}
	}
	if (n == 50000) {
		bool c = 0;
		for (int i = 0; i < x; i++) {
			for (int j = 0; j < x; j++)
				if (base[i][j] < 0)
					c = 1;
			if (c)
				cout << "YES" << endl;
		}
	}
	for (ll i = 0, k = x - 1; i < x; i++, k--)
		a1[i][0] = mark[i];
	power(b - 1, base);
	mul(base, a1, ans);
	cout << ans[x - (k + 1)][0];
}


#include <bits/stdc++.h>
using namespace std;
typedef int mat[200][200];
typedef long long ll;

const ll N = 1e2 + 10, mod = 1e9 + 7;
ll n, b, k, x, mark[N + 10];
mat base, a1, ans;

void mul(mat a, mat b, mat c) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
				c[i][j] = 0;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			for (ll k = 0; k < N; k++)
				c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
}

void swp(mat a, mat b) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			swap(a[i][j], b[i][j]);
}

void power(ll k, mat a) {
	mat b, c;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++) {
				if (i == j)
					b[i][j] = 1;
				else
					b[i][j] = 0;
			}
	for (; k; k /= 2) {
		if (k % 2) {
			mul(a, b, c);
			swp(b, c);
		}
		mul(a, a, c);
		swp(a, c);
	}
	swp(a, b);
}

int main() {
	cin >> n >> b >> k >> x;
	for (ll i = 0; i < n; i++) {
		ll a;
		cin >> a;
		a %= x;
		mark[a]++;
	}
	if (b == 1)
		return cout << mark[k], 0;
	for (ll i = 0, u = x - 1; i < x; i++, u--) {
		for (int j = 0, l = x - 1; j < x; j++, l--) {
			for (int k = 0; k < x; k++) {
				if ((10 * l + k) % x == u)
					base[i][j] += mark[k];
			}
		}
	}
//	for (int i = 0; i < x; i++) {
//		for (int j = 0; j < x; j++)
//			cout << base[i][j] << " ";
//		cout << endl;
//	}
	for (ll i = 0, k = x - 1; i < x; i++, k--)
		a1[i][0] = mark[i];
	power(b - 1, base);
	mul(base, a1, ans);
	cout << ans[x - (k + 1)][0];
}


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

const ll N = 1e2, mod = 1e9 + 7;
ll n, b, k, x, mark[N + 10];
mat base, a1, ans;

void mul(mat a, mat b, mat c) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
				c[i][j] = 0;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			for (ll k = 0; k < N; k++)
				c[i][j] = (c[i][j] + (a[i][k] * b[k][j]) % mod) % mod;
}

void swp(mat a, mat b) {
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++)
			swap(a[i][j], b[i][j]);
}

void power(ll k, mat a) {
	mat b, c;
	for (ll i = 0; i < N; i++)
		for (ll j = 0; j < N; j++) {
				if (i == j)
					b[i][j] = 1;
				else
					b[i][j] = 0;
			}
	for (; k; k /= 2) {
		if (k % 2) {
			mul(a, b, c);
			swp(b, c);
		}
		mul(a, a, c);
		swp(a, c);
	}
	swp(a, b);
}

int main() {
	cin >> n >> b >> k >> x;
	for (ll i = 0; i < n; i++) {
		ll a;
		cin >> a;
		a %= x;
		mark[a]++;
	}
	if (b == 1)
		return cout << mark[k], 0;
	for (ll i = 0, u = x - 1; i < x; i++, u--) {
		for (ll j = 0, l = x - 1; j < x; j++, l--) {
			for (ll k = 0; k < x; k++) {
				if ((10 * l + k) % x == u)
					base[i][j] = (base[i][j] + mark[k]) % mod;
			}
		}
	}
	for (ll i = 0, k = x - 1; i < x; i++, k--)
		a1[i][0] = mark[i];
	power(b - 1, base);
	mul(base, a1, ans);
	cout << ans[x - (k + 1)][0];
}


#include<bits/stdc++.h>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
     int n,i=1,m,k=0,c=0;
     cin>>n;
     for(;i<=n;i++)
     {
          cin>>m>>k;
          if(m==k)
          c++;
     }
     cout<<c;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<string.h>
int a[100001],b[100001];
using namespace std;
int main()
{
     int n,i=1,m[100001],k[100001],c=0;
     cin>>n;
     for(;i<=n;i++)
     {
          cin>>m[i]>>k[i];
          a[k[i]]++;
     }
     for(i=1;i<=n;i++)
     {
          int y=0;
          for(int j=1;j<=n;j++)
          {
               if(i!=j&&m[j]==k[i])
               {
                    y++;
                    break;
               }
          }
          if(!y)
          c++;
     }
     cout<<c;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<string.h>
int a[1001],b[1001];
using namespace std;
int main()
{
     int n,i=1,m[101],k[101],c=0;
     cin>>n;
     for(;i<=n;i++)
     {
          cin>>m[i]>>k[i];
          a[m[i]]++;
     }
     for(i=1;i<=n;i++)
     {
          if(m[i]==k[i])
          a[m[i]]--;
          if(!a[k[i]])
          c++;
     }
     cout<<c;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<string.h>
int a[1001],b[1001];
using namespace std;
int main()
{
     int n,i=1,m[101],k[101],c=0;
     cin>>n;
     int x=n;
     for(;i<=n;i++)
     {
          cin>>m[i]>>k[i];
          a[m[i]]++;
     }
     for(i=1;i<=n;i++)
     {
          if(m[i]==k[i])
          a[m[i]]--;
          if(a[k[i]])
          {
               x-=a[k[i]];
               a[k[i]]=0;
               //b[k[i]]++;
          }

     }
     cout<<x;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<string.h>
int a[1001],b[1001];
using namespace std;
int main()
{
     int n,i=1,m[101],k[101],c=0;
     cin>>n;
     for(;i<=n;i++)
     {
          cin>>m[i]>>k[i];
          a[k[i]]++;
     }
     for(i=1;i<=n;i++)
     {
          if(m[i]==k[i])
          a[k[i]]--;
          if(!a[k[i]])
          c++;
     }
     cout<<c;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const long long M = (long long)1e9 + 7;
#define     ll      long long
#define     ull     unsigned long long
int Int() {
    int x;
    scanf("%d",&x);
    return x;
}
ll Long() {
    ll x;
    scanf("%lld",&x);
    return x;
}
void Char(char* ch) {
    scanf("%s",ch);
}
template<typename t>inline t abs(t a) {
    if(a>=0)return a;
    return -a;
}
template<typename T> inline T modpow(T b, T p, T Mod) {
    T res  = 1;
    while(p) {
        if(p&1) {
            res *= b ;
            res %= Mod ;
        }
        b *= b;
        b %= Mod;
        p >>= 1;
    }
    return res ;
}

template<typename T> inline T ModInv(T b, T Mod) {
    return modpow(b, Mod - 2, Mod );
}

template<typename T> inline T pwr(T b, T p) {
    T res  = 1;
    while(p) {
        if(p&1) {
            res *= b ;
        }
        b *= b;
        p >>= 1;
    }
    return res ;
}

template<typename T> inline T gcd(T a, T b) {
    if(b==0) return a;
    return gcd(b, a%b);
}

template <typename T> inline void Int(T &n) {
    n = 0;
    int f = 1;
    register int ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1;
    for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0';
    n = n * f;
}
template<typename T, typename U> inline void Int(T &a, U &b) {
    Int(a);
    Int(b);
}
template<typename T, typename U, typename W> inline void Int(T &a, U &b, W &c) {
    Int(a,b);
    Int(c);
}
template<typename T, typename U, typename W,typename X> inline void Int(T &a, U &b, W &c, X &d) {
    Int(a,b,c);
    Int(d);
}
vector<int> Unique(vector<int> v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()),v.end());
    return v;
}
///error///
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s,char c) {
    vector<string>v;
    stringstream ss(s);
    string x;
    while(getline(ss,x,c))
        v.emplace_back(x);
    return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T,typename... Args>
void err(vector<string>::iterator it,T a,Args...args) {
    cout<<it->substr((*it)[0]==' ',it->length())<< " = " << a << " ";
    err(++it,args...);
}
///This DEBUGGER-tool need c++11///
#define     Int         Int()
#define     Long        Long()
#define     pii         pair<int,int>
#define     ff          first
#define     ss          second
#define     vi          vector<int>
#define     vll         vector<ll>
#define     vs          vector<string>
#define     pb          push_back
#define     mii         map<int,int>
#define     msi         map<string,int>
#define     mll         map<ll,ll>
#define     muli        map<ull,int>
#define     mli         map<ll,int>
#define     mil         map<int,ll>
#define     umii        unordered_map<int,int>
#define     umsi        unordered_map<string,int>
#define     umll        unordered_map<ll,ll>
#define     umuli       unordered_map<ull,int>
#define     umli        unordered_map<ll,int>
#define     umil        unordered_map<int,ll>
#define     si          set<int>
#define     sll         set<ll>
#define     PQi         priority_queue<int>
#define     PQl         priority_queue<ll>
#define     all(x)      x.begin(),x.end()
#define     LB          lower_bound
#define     UB          upper_bound
#define     Mx(a,b)     (a>b)?a:b
#define     Mn(a,b)     (a<b)?a:b
#define     mem(a)      memset(a,0,sizeof a)
#define     memn(a)     memset(a,-1,sizeof a)

int a[103],b[104],cnt[1003];
int main() {
    int n = Int;
    for(int i = 1 ; i <= n ; i++) {
        a[i] = Int , b[i] = Int ;
    }
    for(int i = 1 ; i <= n ; i++) {
        for(int j = 1 ; j <= n ; j++) {
            if(i == j)continue;
            if(a[j] == b[i]) {
                cnt[j] = 1 ;
            }
        }
    }
    int res = 0 ;
    for(int i = 1 ; i <= 1000; i++){
        if(cnt[i])res++;
    }
    printf("%d\n",n-res);
    return 0;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   cin>>n;
   int c[1001]={0},v[1001]={0};
   memset(v,-1,sizeof(v));
   for(int i=0;i<n;i++)
   {
   	int a,b;
   	cin>>a>>b;
   	c[a]++;
   	if(v[a]==-1)
   		v[a]=0;
   	if(v[b]==-1)
   		v[b]=0;
   	if(v[b]==0)
   		v[b]=a;
   	else if(v[b]==b)
   		v[b]=a;
   }
   int ans=0;
   for(int i=1;i<=1000;i++)
   {
   	if(v[i]==0)
   		ans+=c[i];
   	else if(v[i]==i&&c[i]<=1)
   		ans=ans+1;
   }
   cout<<ans<<endl;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
   // cout<<x<<' '<<y<<endl;
    if(y==0)
    return x;
    else
    return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
    if(m%2)
        r=(r*n)%p;
    n=(n*n)%p;
    m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
    if (n%i == 0 || n%(i+2) == 0)
       return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
    if (i <= r)
        z[i] = min (r - i + 1, z[i - l]);
    while (i + z[i] < n && s[z[i]] == s[i + z[i]])
        ++z[i];
    if (i + z[i] - 1 > r)
        l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>adj[1005];
bool vis[1005];
void dfs(int s)
{
	//debug1(s);
	vis[s]=true;
	for(int i=0;i<adj[s].size();i++)
	{
		int x=adj[s][i];
		if(!vis[x])
			dfs(x);
	}
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   cin>>n;
   int s[1001]={0},v[1001]={0};
   memset(v,0,sizeof(v));
   vector<pair<int,int> >c(n);
   for(int i=0;i<n;i++)
   {
   	int a,b;
   	cin>>a>>b;
   	c[i].ff=a,c[i].ss=b;
   }
   int ans=0;
   for(int i=0;i<n;i++)
   {
   	for(int j=0;j<n;j++)
   	{
   		if(c[i].ss==c[j].ff&&i!=j)
   		{
   			v[j]=1;
   		}
   	}
   }
   for(int i=0;i<n;i++)
   {
   	if(v[i]==0)
   		ans++;
   }
   cout<<ans<<endl;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   cin>>n;
   int s[1001]={0},v[1001]={0};
   memset(v,0,sizeof(v));
   vector<pair<int,int> >c(n);
   for(int i=0;i<n;i++)
   {
   	int a,b;
   	cin>>a>>b;
   	c[i].ff=a,c[i].ss=b;
   }
   int ans=0;
   for(int i=0;i<n;i++)
   {
   	for(int j=0;j<n;j++)
   	{
   		if(c[i].ss==c[j].ff&&i!=j)
   		{
   			v[j]=1;
   		}
   	}
   	if(v[i]==0)
   		ans++;
   }
   cout<<ans<<endl;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n,c=0;
   cin>>n;
   bool v[1001];
   memset(v,false,sizeof(v));
   while(n--)
   {
       int a,b;
       cin>>a>>b;
       v[b]=true;
       if(a==b)
       		v[b]=false;
   }
   for(int i=1;i<=n;i++)
   {
       if(v[i]==false)
        c++;
   }
   cout<<c;
   return 0;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
   // cout<<x<<' '<<y<<endl;
    if(y==0)
    return x;
    else
    return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
    if(m%2)
        r=(r*n)%p;
    n=(n*n)%p;
    m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
    if (n%i == 0 || n%(i+2) == 0)
       return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
    if (i <= r)
        z[i] = min (r - i + 1, z[i - l]);
    while (i + z[i] < n && s[z[i]] == s[i + z[i]])
        ++z[i];
    if (i + z[i] - 1 > r)
        l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>adj[1005];
bool vis[1005];
void dfs(int s)
{
	//debug1(s);
	vis[s]=true;
	for(int i=0;i<adj[s].size();i++)
	{
		int x=adj[s][i];
		if(!vis[x])
			dfs(x);
	}
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   cin>>n;
   bool uf[1005];
   memset(uf,false,sizeof(uf));
   memset(vis,false,sizeof(vis));
   for(int i=0;i<n;i++)
   {
   	int x,y;
   	cin>>x>>y;
   	adj[x].pb(y);
   	uf[x]=uf[y]=true;
   }
   int ans=0;
   for(int i=1;i<=1005;i++)
   {
   	if(!uf[i])
   		continue;
   	if(!vis[i])
   	{
   		ans++;
   		dfs(i);
   	}
   }
   cout<<ans;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   cin>>n;
   int s[1001]={0},v[1001]={0};
   memset(v,0,sizeof(v));
   vector<pair<int,int> >c(n);
   for(int i=0;i<n;i++)
   {
   	int a,b;
   	cin>>a>>b;
   	c[i].ff=a,c[i].ss=b;
   }
   int ans=0;
   for(int i=0;i<n;i++)
   {
   	for(int j=0;j<n;j++)
   	{
   		if(c[i].ss==c[j].ff&&i!=j)
   		{
   			v[j]=1;
   			//cout<<j<<endl;
   		}
   	}
   }
   for(int i=0;i<n;i++)
   {
   	if(v[i]==0)
   		ans++;
   }
   cout<<ans<<endl;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   cin>>n;
   int c[1001]={0},v[1001]={0};
   memset(v,-1,sizeof(v));
   for(int i=0;i<n;i++)
   {
   	int a,b;
   	cin>>a>>b;
   	c[a]++;
   	if(v[a]==-1)
   		v[a]=0;
   	if(v[b]==-1)
   		v[b]=0;
   	if(v[b]==0)
   		v[b]=a;
   	else if(v[b]==b)
   		v[b]=a;
   }
   int ans=0;
   for(int i=1;i<=1000;i++)
   {
   	if(v[i]==0)
   		ans+=c[i];
   	else if(v[i]==i)
   		ans=ans+1;
   }
   cout<<ans<<endl;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
   // cout<<x<<' '<<y<<endl;
    if(y==0)
    return x;
    else
    return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
    if(m%2)
        r=(r*n)%p;
    n=(n*n)%p;
    m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
    if (n%i == 0 || n%(i+2) == 0)
       return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
    if (i <= r)
        z[i] = min (r - i + 1, z[i - l]);
    while (i + z[i] < n && s[z[i]] == s[i + z[i]])
        ++z[i];
    if (i + z[i] - 1 > r)
        l = i, r = i + z[i] - 1;
    }
    return z;
}
vector<int>adj[1005];
bool vis[1005];
void dfs(int s)
{
	//debug1(s);
	vis[s]=true;
	for(int i=0;i<adj[s].size();i++)
	{
		int x=adj[s][i];
		if(!vis[x])
			dfs(x);
	}
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   cin>>n;
   bool uf[1005];
   memset(uf,false,sizeof(uf));
   memset(vis,false,sizeof(vis));
   for(int i=0;i<n;i++)
   {
   	int x,y;
   	cin>>x>>y;
   	adj[x].pb(y);
   	uf[x]=uf[y]=true;
   }
   int ans=0;
   for(int i=1;i<1005;i++)
   {
   	if(!uf[i])
   		continue;
   	if(!vis[i])
   	{
   		ans++;
   		dfs(i);
   	}
   }
   cout<<ans;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n,c=0;
   cin>>n;
   bool v[1001];
   memset(v,false,sizeof(v));
   for(int i=0;i<n;i++)
   {
       int a,b;
       cin>>a>>b;
       v[b]=true;
       if(a==b)
       		v[b]=false;
   }
   for(int i=1;i<=n;i++)
   {
       if(v[i]==false)
        c++;
	//cout<<v[i]<<endl;
   }
   cout<<c;
   return 0;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n;
   cin>>n;
   int c[1001]={0},v[1001]={0};
   memset(v,-1,sizeof(v));
   for(int i=0;i<n;i++)
   {
   	int a,b;
   	cin>>a>>b;
   	c[a]++;
   	if(v[a]==-1)
   		v[a]=0;
   	if(v[b]==-1)
   		v[b]=0;
   	if(v[b]==0)
   		v[b]=a;
   	else if(v[b]==b)
   		v[b]=a;
   }
   int ans=0;
   for(int i=1;i<=1000;i++)
   {
   	if(v[i]==0)
   		ans+=c[i];
   	else if(v[i]==i)
   		ans=ans+c[i]-1;
   }
   cout<<ans<<endl;
}


#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ff first
#define ss second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define int long long
#define all(a) a.begin(), a.end()
#define inf (1LL<<61)
#define print_time cerr << "Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
#define ull unsigned long long
#define debug1(x) cerr << #x << ": " << x <<'\n'
#define debug2(x, y) cerr << #x << ": " << x << '\t' << #y << ": " << y <<'\n'
#define debug3(x, y, z) cerr << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " << z <<'\n'
using namespace std;
int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
int gcd(int x,int y)
{
    if(y==0)
	return x;
    else
	return gcd(y,x%y);
}
int expo(int n,int m,int p)   //modULAR EXPONENTIATION
{
    int r=1;
    n=n%p;
    while(m>0)
    {
	if(m%2)
	    r=(r*n)%p;
	n=(n*n)%p;
	m=m/2;
    }
    return r%p;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
	if (n%i == 0 || n%(i+2) == 0)
	   return false;

    return true;
}
vector<int> z_function(string s) {
    int n = (int) s.length();
    vector<int> z(n);
    for (int i = 1, l = 0, r = 0; i < n; ++i) {
	if (i <= r)
	    z[i] = min (r - i + 1, z[i - l]);
	while (i + z[i] < n && s[z[i]] == s[i + z[i]])
	    ++z[i];
	if (i + z[i] - 1 > r)
	    l = i, r = i + z[i] - 1;
    }
    return z;
}
int32_t main()
{
   IOS;
   #ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
   #endif
   /********************* Code starts here ***********************/
   int n,c=0;
   cin>>n;
   int v[1001];
   memset(v,-1,sizeof(v));
   for(int i=0;i<n;i++)
   {
       int a,b;
       cin>>a>>b;
       if(v[a]==-1)
       	v[a]=0;
       if(v[b]==-1)
       	v[b]=0;
       v[b]=true;
       if(a==b)
       		v[b]=false;
   }
   for(int i=1;i<=1000;i++)
   {
       if(v[i]==0)
        c++;
	//cout<<v[i]<<endl;
   }
   cout<<c;
   return 0;
}


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

int main()
{
	int n;
	cin>>n;

	int arr[n],arr1[n],mark[1001]={0};
	vector <int > v;
	int count=0;
	for(int i=0;i<n;i++)
	{
		cin>>arr[i]>>arr1[i];
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(i==j&&arr[i]==arr1[j])continue;
			if(arr1[i]==arr[j]&&mark[arr1[i]]==0)
			{
				//cout<<arr1[i]<<" index in second array "<<i<<"& "<<arr[j]<<" index in first array "<<j<<endl;
				mark[arr1[i]]++;
				v.push_back(arr1[i]);
			}

		}
	}
	//cout<<"v.size()"<<v.size()<<endl;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<v.size();j++)
		{
			if(arr[i]==v[j])count++;
		}
	}
	cout<<n-count;

}


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

int main()
{
	int n;
	cin>>n;

	int arr[n],arr1[n];
	int count=n;
	for(int i=0;i<n;i++)
	{
		cin>>arr[i]>>arr1[i];
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(i==j)continue;
			if(arr1[i]==arr[j])
			{
				count--; continue;
			}
		}
	}
	cout<<count;

}


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

int main()
{
	int n;
	cin>>n;

	int arr[n],arr1[n],mark[1001]={0};
	vector <int > v;
	int count=0;
	for(int i=0;i<n;i++)
	{
		cin>>arr[i]>>arr1[i];
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(i==j)continue;
			if(arr1[i]==arr[j]&&mark[arr1[i]]==0)
			{
				//cout<<arr1[i]<<" index in second array "<<i<<"& "<<arr[j]<<" index in first array "<<j<<endl;
				mark[arr1[i]]++;
				v.push_back(arr1[i]);
			}

		}
	}
	//cout<<"v.size()"<<v.size()<<endl;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<v.size();j++)
		{
			if(arr[i]==v[j])count++;
		}
	}
	cout<<n-count;

}


#include <bits/stdc++.h>
#define ll long long int
#define loop(i,a,b) for(ll i=a;i<b;i++)
using namespace std;

/* Memento Mori
Remember
you are but a mortal*/

//315A
//I cant believe it i was missing the obvious and
//This was so simple it slipped past my nose!!!
//BIGGESTR MISTAKE LOOKED AT THE RATING AND GOT SCARED IT IS EASY AF
//***FACEPALM***
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
  	int n;
  	cin>>n;

  	int ar[n],ar1[n];
  	loop(i,0,n)
  	cin>>ar[i]>>ar1[i];
  	int s=0;

  	loop(i,0,n)
  	{
  		loop(j,0,n)
  		{
  			if(i!=j)
  			{
  				if(ar[i]==ar1[j]){s++; break;}
  			}
  		}
  	}

  	cout<<n-s;
}


#include <bits/stdc++.h>
#define ll long long int
#define loop(i,a,b) for(ll i=a;i<b;i++)
using namespace std;

/* Memento Mori
Remember
you are but a mortal*/

//315A
//I cant believe it i was missing the obvious and
//This was so simple it slipped past my nose!!!
//BIGGESTR MISTAKE LOOKED AT THE RATING AND GOT SCARED IT IS EASY AF
//***FACEPALM***
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
  	int n;
  	cin>>n;

  	int ar[n],ar1[n];
  	loop(i,0,n)
  	cin>>ar[i]>>ar1[i];
  	int s=0;

  	loop(i,0,n)
  	{
  		loop(j,0,n)
  		{
  			if(ar[j]==ar1[i]&&i!=j)
  				s++;
  			break;
  		}
  	}

  	cout<<n-s;
}


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

int main()
{
	int n;
	cin>>n;

	int arr[n],arr1[n],mark[101]={0};
	vector <int > v;
	int count=0;
	for(int i=0;i<n;i++)
	{
		cin>>arr[i]>>arr1[i];
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(i==j)continue;
			if(arr1[i]==arr[j]&&mark[arr1[i]]==0)
			{
				//cout<<arr1[i]<<" index in second array "<<i<<"& "<<arr[j]<<" index in first array "<<j<<endl;
				mark[arr1[i]]++;
				v.push_back(arr1[i]);
			}

		}
	}
	//cout<<"v.size()"<<v.size()<<endl;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<v.size();j++)
		{
			if(arr[i]==v[j])count++;
		}
	}
	cout<<n-count;

}


#include <bits/stdc++.h>
#define ll long long int
#define loop(i,a,b) for(ll i=a;i<b;i++)
using namespace std;

/* Memento Mori
Remember
you are but a mortal*/

//315A
//I cant believe it i was missing the obvious and
//This was so simple it slipped past my nose!!!
//BIGGESTR MISTAKE LOOKED AT THE RATING AND GOT SCARED IT IS EASY AF
//***FACEPALM***
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
  	int n;
  	cin>>n;

  	int ar[n],ar1[n];
  	loop(i,0,n)
  	cin>>ar[i]>>ar1[i];
  	int s=0;

  	loop(i,0,n)
  	{
  		loop(j,0,n)
  		{
  			if(ar[i]==ar1[j]&&i!=j)
  				s++;
  			break;
  		}
  	}

  	cout<<n-s;
}


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

int main()
{
	int n;
	cin>>n;

	int arr[n][2];
	int count=0;
	for(int i=0;i<n;i++)
	{
		cin>>arr[i][0]>>arr[i][1];
		if(arr[i][0]==arr[i][1])count++;
	}
	cout<<count;

}


#include<bits/stdc++.h>
#include<stdio.h>
#include <string>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int counter=0;
  int arr[10004];
  vector <int> a,b;
  for(int i=0;i<n;i++)
  {
      int x,y;
      cin>>x>>y;
      a.push_back(x);
      b.push_back(y);
  }
  for(int i=0;i<n;i++)
  {
      if((a[i]!=b[i])){arr[b[i]]=1;}
      else if((a[i]==b[i])&&(count(a.begin(),a.end(),b[i])>1)){arr[b[i]]=1;}
  }
  for(int i=0;i<n;i++)
  {
      if(arr[a[i]]==1)counter++;
  }
  cout<<n-counter;
 }


#include<bits/stdc++.h>
#include<stdio.h>
#include <string>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int counter=0;
  int arr[1004];
  vector <int> a,b;
  for(int i=0;i<n;i++)
  {
      int x,y;
      cin>>x>>y;
      a.push_back(x);
      b.push_back(y);
  }
  for(int i=0;i<n;i++)
  {
      if((a[i]!=b[i])){arr[b[i]]=1;}
      else if((a[i]==b[i])&&(count(a.begin(),a.end(),b[i])>1)){arr[b[i]]=1;}
  }
  for(int i=0;i<n;i++)
  {
      if(arr[a[i]]==1)counter++;
  }
  cout<<n-counter;

}


#include<bits/stdc++.h>
#include<stdio.h>
#include <string>
#define N 1000000000
using namespace std;
int main()
{
  int n;
  cin>>n;
  int count=0;
  for(int i=0;i<n;i++)
  {
      int x,y;
      cin>>x>>y;
      if(x==y) count++;
  }
  cout<<count;

}


#include<bits/stdc++.h>
#include<stdio.h>
#include <string>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int counter=0;
  int arr[1000];
  vector <int> a,b;
  for(int i=0;i<n;i++)
  {
      int x,y;
      cin>>x>>y;
      a.push_back(x);
      b.push_back(y);
  }
  for(int i=0;i<n;i++)
  {
      if((a[i]!=b[i])&&(find(a.begin(),a.end(),b[i])!=a.end())){arr[a[i]]=1;}
      else if((a[i]==b[i])&&(count(a.begin(),a.end(),b[i])>1)){arr[a[i]]=1;}
  }
  for(int i=0;i<n;i++)
  {
      if(arr[a[i]]==1)counter++;
  }
  cout<<n-counter;

}


#include<bits/stdc++.h>
#include<stdio.h>
#include <string>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int counter=0;
  int arr[1007];
  vector <int> a,b;
  for(int i=0;i<n;i++)
  {
      int x,y;
      cin>>x>>y;
      a.push_back(x);
      b.push_back(y);
  }
  for(int i=0;i<n;i++)
  {
      if((a[i]!=b[i])){arr[b[i]]=1;}
      else if((a[i]==b[i])&&(count(a.begin(),a.end(),b[i])>1)){arr[b[i]]=1;}
  }
  for(int i=0;i<n;i++)
  {
      if(arr[a[i]]==1)counter++;
  }
  cout<<n-counter;

}


#include<bits/stdc++.h>
#include<stdio.h>
#include <string>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int counter=0;
  int arr[1000];
  vector <int> a,b;
  for(int i=0;i<n;i++)
  {
      int x,y;
      cin>>x>>y;
      a.push_back(x);
      b.push_back(y);
  }
  for(int i=0;i<n;i++)
  {
      if((a[i]!=b[i])){arr[b[i]]=1;}
      else if((a[i]==b[i])&&(count(a.begin(),a.end(),b[i])>1)){arr[b[i]]=1;}
  }
  for(int i=0;i<n;i++)
  {
      if(arr[a[i]]==1)counter++;
  }
  cout<<n-counter;

}


//BROWNIE TK

#include <bits/stdc++.h>
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ldb;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define popb pop_back()
#define pb push_back
#define popf pop_front()
#define pf push_front
#define be begin()
#define en end()
#define si size()
#define all(v) v.be, v.en
#define le length()
#define mp make_pair
#define mt make_tuple
#define acc(v) accumulate(all(v), 0)
#define F first
#define S second

#define forz(i, n) for (int i = 0; i < n; i++)
#define fore(i, m, n) for (int i = m; i <= n; i++)
#define rforz(i, n) for (int i = n - 1; i >= 0; i--)
#define rforzm(i, m, n) for (int i = n - 1; i >= m; i--)
#define deci(n) fixed << setprecision(n)
#define high(n) __builtin_popcount(n)
#define highll(n) __builtin_popcountll(n)
#define parity(n) __builtin_parity(n)
#define ctz(n) __builtin_ctz(n)
#define lb lower_bound
#define ub upper_bound
#define er equal_range
#define maxe *max_element
#define mine *min_element
#define mod 1000000007
#define mod2 998244353
#define gcd __gcd
#define kira ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)

#define endl "\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl

#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define osetlli tree<lli, null_type, less<lli>, rb_tree_tag, tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
#define ofk order_of_key
#define fbo find_by_order
using namespace std;

/*STD fucntions*/
lli power(lli x, lli y, lli p)
{
    lli res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        //y must be even now
        y = y >> 1; //y=y/2
        x = (x * x) % p;
    }
    return res;
}
lli modi(lli a, lli m)
{
    // fermat little thm where m is prime
    return power(a, m - 2, m);
}
/*CODE BEGINS*/
struct Node
{
    int counter;
    int open;
};
 int main()
 {

   int n;
   cin>>n;
   int a[n],b[n];
   forz(i,n)
   {
       cin>>a[i]>>b[i];
   }
   int nt=0;
   forz(i,n)
   {
       forz(j,n)
       {
           if(a[i]==b[j]&&i!=j){nt++;break;}
       }
   }
   cout<<n-nt;

 }


///*Bismillahir Rahmanir Rahim***///
/***Stay_Home_Stay_Safe***/
///**Author Thasin Sheikh**///
#include<bits/stdc++.h>
using namespace std;
#define MAX 1e6+10
#define MOD 1000000007
#define PI 3.14159265359
#define makefast__  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using ll=long long int  ;
using dl =double;
using ull=unsigned long long;
const int N = 2e5 + 10;
ll a[N],b[N];
ll magic[505][505];
vector<ll>primes;
bool ok[1000001];
map<ll,ll>mp;
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    makefast__
    string str;
    ll i,j,n,m,k,t,ans=0,f=0;
    set<ll>sl;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(i!=j&&b[i]==a[j])
            {
                a[j]=-1;
                f++;
            }
        }
    }
    cout<<n-f<<endl;

    return 0;
}


///*Bismillahir Rahmanir Rahim***///
/***Stay_Home_Stay_Safe***/
///**Author Thasin Sheikh**///
#include<bits/stdc++.h>
using namespace std;
#define MAX 1e6+10
#define MOD 1000000007
#define PI 3.14159265359
#define makefast__  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using ll=long long int  ;
using dl =double;
using ull=unsigned long long;
const int N = 2e5 + 10;
ll a[N],b[N];
ll magic[505][505];
vector<ll>primes;
bool ok[1000001];
map<ll,ll>mp;
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    makefast__
    string str;
    ll i,j,n,m,k,t,ans=0;
    set<ll>sl;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
        sl.insert(a[i]);
    }
    for(i=1;i<=n;i++)
    {
        if(sl.find(b[i])!=sl.end()&&a[i]!=b[i])
        {
            mp[a[i]]++;
            mp[b[i]]++;
        }
    }
    for(i=1;i<=n;i++)
    {
        if(mp[a[i]]==0)
        {
            ans++;
        }
    }
    cout<<ans<<endl;

    return 0;
}


///*Bismillahir Rahmanir Rahim***///
/***Stay_Home_Stay_Safe***/
///**Author Thasin Sheikh**///
#include<bits/stdc++.h>
using namespace std;
#define MAX 1e6+10
#define MOD 1000000007
#define PI 3.14159265359
#define makefast__  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using ll=long long int  ;
using dl =double;
using ull=unsigned long long;
const int N = 2e5 + 10;
ll a[N],b[N];
ll magic[505][505];
vector<ll>primes;
bool ok[1000001];
map<ll,ll>mp;
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    makefast__
    string str;
    ll i,j,n,m,k,t,ans=0;
    set<ll>sl;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
        if(a[i]!=b[i])
        mp[b[i]]++;
    }
    for(i=1;i<=n;i++)
    {
        if(mp[a[i]]==0)
        {
            ans++;
        }
    }
    cout<<ans<<endl;

    return 0;
}


///*Bismillahir Rahmanir Rahim***///
/***Stay_Home_Stay_Safe***/
///**Author Thasin Sheikh**///
#include<bits/stdc++.h>
using namespace std;
#define MAX 1e6+10
#define MOD 1000000007
#define PI 3.14159265359
#define makefast__  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using ll=long long int  ;
using dl =double;
using ull=unsigned long long;
const int N = 2e5 + 10;
ll a[N],b[N];
ll magic[505][505];
vector<ll>primes;
bool ok[1000001];
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    makefast__
    string str;
    ll i,j,n,m,k,t,ans=0;
    set<ll>sl;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
        sl.insert(a[i]);
    }
    for(i=1;i<=n;i++)
    {
        if(sl.find(b[i])!=sl.end()&&a[i]!=b[i])
        {
            continue;
        }
        ans++;
    }
    cout<<ans<<endl;

    return 0;
}


///*Bismillahir Rahmanir Rahim***///
/***Stay_Home_Stay_Safe***/
///**Author Thasin Sheikh**///
#include<bits/stdc++.h>
using namespace std;
#define MAX 1e6+10
#define MOD 1000000007
#define PI 3.14159265359
#define makefast__  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using ll=long long int  ;
using dl =double;
using ull=unsigned long long;
const int N = 2e5 + 10;
ll a[N],b[N];
ll magic[505][505];
vector<ll>primes;
bool ok[1000001];
map<ll,ll>mp;
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    makefast__
    string str;
    ll i,j,n,m,k,t,ans=0,f=0;
    set<ll>sl;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(i!=j&&b[i]==a[j]&&mp[a[j]]==0)
            {
                mp[a[j]]++;
                f++;
            }
        }
    }
    cout<<n-f<<endl;

    return 0;
}


///*Bismillahir Rahmanir Rahim***///
/***Stay_Home_Stay_Safe***/
///**Author Thasin Sheikh**///
#include<bits/stdc++.h>
using namespace std;
#define MAX 1e6+10
#define MOD 1000000007
#define PI 3.14159265359
#define makefast__  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using ll=long long int  ;
using dl =double;
using ull=unsigned long long;
const int N = 2e5 + 10;
ll a[N],b[N];
ll magic[505][505];
vector<ll>primes;
bool ok[1000001];
map<ll,ll>mp;
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    makefast__
    string str;
    ll i,j,n,m,k,t,ans=0,f=0;
    set<ll>sl;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(i!=j&&b[i]==a[j])
            {
                f++;
            }
        }
    }
    cout<<n-f<<endl;

    return 0;
}


///*Bismillahir Rahmanir Rahim***///
/***Stay_Home_Stay_Safe***/
///**Author Thasin Sheikh**///
#include<bits/stdc++.h>
using namespace std;
#define MAX 1e6+10
#define MOD 1000000007
#define PI 3.14159265359
#define makefast__  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using ll=long long int  ;
using dl =double;
using ull=unsigned long long;
const int N = 2e5 + 10;
ll a[N],b[N];
ll magic[505][505];
vector<ll>primes;
bool ok[1000001];
map<ll,ll>mp;
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    makefast__
    string str;
    ll i,j,n,m,k,t,ans=0;
    set<ll>sl;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
    }
    for(i=1;i<=n;i++)
    {
        ll f=1;
        for(j=1;j<=n;j++)
        {
            if(i!=j&&b[i]==a[j])
            {
                f=0;
                break;
            }
        }
        if(f)
            ans++;
    }
    cout<<ans<<endl;

    return 0;
}


///*Bismillahir Rahmanir Rahim***///
/***Stay_Home_Stay_Safe***/
///**Author Thasin Sheikh**///
#include<bits/stdc++.h>
using namespace std;
#define MAX 1e6+10
#define MOD 1000000007
#define PI 3.14159265359
#define makefast__  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using ll=long long int  ;
using dl =double;
using ull=unsigned long long;
const int N = 2e5 + 10;
ll a[N],b[N];
ll magic[505][505];
vector<ll>primes;
bool ok[1000001];
map<ll,ll>mp;
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    makefast__
    string str;
    ll i,j,n,m,k,t,ans=0;
    set<ll>sl;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
        sl.insert(a[i]);
    }
    for(i=1;i<=n;i++)
    {
        if(sl.find(b[i])!=sl.end()&&a[i]!=b[i])
        {
            mp[a[i]]++;
        }
    }
    for(i=1;i<=n;i++)
    {
        if(mp[a[i]]==0)
        {
            ans++;
        }
    }
    cout<<ans<<endl;

    return 0;
}


///*Bismillahir Rahmanir Rahim***///
/***Stay_Home_Stay_Safe***/
///**Author Thasin Sheikh**///
#include<bits/stdc++.h>
using namespace std;
#define MAX 1e6+10
#define MOD 1000000007
#define PI 3.14159265359
#define makefast__  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using ll=long long int  ;
using dl =double;
using ull=unsigned long long;
const int N = 2e5 + 10;
ll a[N],b[N];
ll magic[505][505];
vector<ll>primes;
bool ok[1000001];
map<ll,ll>mp;
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    makefast__
    string str;
    ll i,j,n,m,k,t,ans=0;
    set<ll>sl;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
        sl.insert(a[i]);
    }
    for(i=1;i<=n;i++)
    {
        if(sl.find(b[i])!=sl.end()&&a[i]!=b[i])
        {
            mp[a[i]]++;
        }
    }
    for(auto it=sl.begin();it!=sl.end();it++)
    {
        if(mp[*it]==0)
        {
            ans++;
        }
    }
    cout<<ans<<endl;

    return 0;
}