#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000
#define INT_MINI -9000000000000000000

ll max(ll a, ll b) {if (a > b) return a; else return b;}
ll min(ll a, ll b) {if (a < b) return a; else return b;}

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

int main()
{
	fio;

	ll t,i,j,n,m,l,r,ta,tb,k,x,cl,ce,v,q,x1,y1,x2,y2;

    cin>>n>>m;
    ll arr1[n+1];
    ll arr2[m+1];
    set<ll>s2;
    for(i=1;i<=n;i++)
    {
    	cin>>arr1[i];
    	s2.insert(arr1[i]);
	}
	for(i=1;i<=m;i++)
	{
		cin>>arr2[i];
	}

	ll low=0,high=n,ans=-1;
	while(low<=high)
	{
			set<ll>s;
		for(i=1;i<=m;i++)
		{
			if(s2.find(i)==s2.end()&&arr2[i]==0)
			s.insert(i);
		}
		ll mid=(low+high)>>1;
		i=1,j=1;

		ll count=0;
		map<ll,ll>mpp;
		ll flag=0;
		while(i<=n&&j<=n)
		{
			mpp[arr1[j]]++;

			if(mpp[arr1[j]]>=arr2[arr1[j]])
			s.insert(arr1[j]);
		//	cout<<i<<" "<<j<<" "<<s.size()<<endl;
	         if(arr2[arr1[j]]==0&&mpp[arr1[j]]==1)
				{
					if(s.size()==m)
					{
						flag=1;
						break;
					}
					j++;
					continue;
				}

			if(mpp[arr1[j]]>arr2[arr1[j]])

			{

				if(count<mid)
				{
					count++;
				}
				else
				{
					while(true)
					{
						if(mpp[arr1[i]]>arr2[arr1[i]])
						{
						//	count--;
						    if(arr2[arr1[i]]==0&&mpp[arr1[i]]==1)
						    {
						    		mpp[arr1[i]]--;
							i++;
							}
							else
							{
									mpp[arr1[i]]--;
							i++;
								break;
							}

						}
						else
						{
							mpp[arr1[i]]--;
							if(mpp[arr1[i]]<arr2[arr1[i]])
							s.erase(arr1[i]);
							i++;
						}
					}

				}

			}
			if(s.size()==m)
			{
				flag=1;
				break;
			}
			j++;
		//	cout<<i<<" "<<j<<" "<<s.size()<<endl;

		}
		if(flag==1)
		{
			ans=mid;
			high=mid-1;
		}
		else
		low=mid+1;

	}
	cout<<ans;

}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000
#define INT_MINI -9000000000000000000

ll max(ll a, ll b) {if (a > b) return a; else return b;}
ll min(ll a, ll b) {if (a < b) return a; else return b;}

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

int main()
{
	fio;

	ll t,i,j,n,m,l,r,ta,tb,k,x,cl,ce,v,q,x1,y1,x2,y2;

    cin>>n>>m;
    ll arr1[n+1];
    ll arr2[m+1];
    for(i=1;i<=n;i++)
    {
    	cin>>arr1[i];
	}
	for(i=1;i<=m;i++)
	{
		cin>>arr2[i];
	}

	ll low=0,high=n,ans=-1;
	while(low<=high)
	{
		ll mid=(low+high)>>1;
		i=1,j=1;
		set<ll>s;
		ll count=0;
		map<ll,ll>mpp;
		ll flag=0;
		while(i<=n&&j<=n)
		{
			mpp[arr1[j]]++;

			if(mpp[arr1[j]]==arr2[arr1[j]])
			s.insert(arr1[j]);
		//	cout<<i<<" "<<j<<" "<<s.size()<<endl;
			if(mpp[arr1[j]]>arr2[arr1[j]])
			{
				if(count<mid)
				{
					count++;
				}
				else
				{
					while(true)
					{
						if(mpp[arr1[i]]>arr2[arr1[i]])
						{
						//	count--;
							mpp[arr1[i]]--;
							i++;
							break;
						}
						else
						{
							mpp[arr1[i]]--;
							s.erase(arr1[i]);
							i++;
						}
					}

				}
			}
			if(s.size()==m)
			{
				flag=1;
				break;
			}
			j++;
		//	cout<<i<<" "<<j<<" "<<s.size()<<endl;

		}
		if(flag==1)
		{
			ans=mid;
			high=mid-1;
		}
		else
		low=mid+1;

	}
	cout<<ans;

}


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

int constexpr N = 300009;
int n, m, a[N], k[N];

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; ++i) cin >> a[i];
    int needed = 0;
    for(int j = 1; j <= m; ++j) cin >> k[j], needed += k[j];
    int ans = 1e9, total = needed;

    for(int p1 = 1, p2 = 1; p1 <= n; ) {
        while(p2 <= n) {
            int col = a[p2];
            k[col]--;
            if(k[col] >= 0) needed--;
            p2++;
            if(needed == 0 || p2 > n) {
                break;
            }
        }
        if(needed == 0) {
            ans = min(ans, (p2 - p1) - total);
        }
        while(p1 <= n) {
            int col = a[p1];
            k[col]++;
            p1++;
            if(k[col] > 0) needed++;
            if(needed == 0) ans = min(ans, (p2 - p1) - total);
            if(needed > 0 || p1 == p2) break;
        }
    }

    cout << (ans == 1e9 ? -1 : ans) << endl;
}


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

int constexpr N = 200009;
int n, m, a[N], k[N];

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; ++i) cin >> a[i];
    int needed = 0;
    for(int j = 1; j <= m; ++j) cin >> k[j], needed += k[j];
    int ans = 1e9, total = needed;
    for(int p1 = 1, p2 = 1; p1 <= n; ) {
        while(true) {
            int col = a[p2];
            k[col]--;
            if(k[col] >= 0) needed--;
            p2++;
            if(needed == 0 || p2 > n) {
                break;
            }
        }
        if(needed == 0) {
            ans = min(ans, (p2 - p1) - total);
        }
        while(true) {
            int col = a[p1];
            k[col]++;
            p1++;
            if(k[col] > 0) needed++;
            if(needed == 0) ans = min(ans, (p2 - p1) - total);
            if(needed > 0 || p1 == p2) break;
        }
    }

    cout << (ans == 1e9 ? -1 : ans) << endl;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rev(i,a,b) for(int i=a;i>b;i--)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define se second
#define all(a) (a).begin(),(a).end()
#define endl '\n'
#define MOD 998244353
#define PI 3.14159265358979323846
using namespace std;
int n,m,k[200010],a[200010],cnt[200010],cov,ans=INT_MAX,sum;
//ll sum;
void code(){
    cin>>n>>m;
    rep(i,0,n)
        cin>>a[i];
    rep(i,1,m+1)
        cin>>k[i];
    sum=accumulate(k+1,k+m+1,0LL);
    int l=0,r=0;
    while(r<n || cov==m){
        if(cov==m){
            ans=min(ans,r-l-sum);
            if(cnt[a[l]]==k[a[l]])
                cov--;
            cnt[a[l]]--;
            l++;
        }
        else{
            cnt[a[r]]++;
            if(cnt[a[r]]==k[a[r]])
                cov++;
            r++;
        }
    }
    //if(cov==m)
    cout<<ans<<endl;
}
int main(){
    ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
	//cin>>t;
	while(t--){
        code();
	}
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rev(i,a,b) for(int i=a;i>b;i--)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define se second
#define all(a) (a).begin(),(a).end()
#define endl '\n'
#define MOD 998244353
#define PI 3.14159265358979323846
using namespace std;
int n,m,k[200010],a[200010],cnt[200010],cov,ans=INT_MAX,sum;
//ll sum;
void code(){
    cin>>n>>m;
    rep(i,0,n)
        cin>>a[i];
    rep(i,1,m+1){
        cin>>k[i];
        if(k[i]==0)
            cov++;
    }
    sum=accumulate(k+1,k+m+1,0LL);
    int l=0,r=0;
    while(r<n || cov==m){
        if(cov==m){
            ans=min(ans,r-l-sum);
            if(cnt[a[l]]==k[a[l]])
                cov--;
            cnt[a[l]]--;
            l++;
        }
        else{
            cnt[a[r]]++;
            if(cnt[a[r]]==k[a[r]])
                cov++;
            r++;
        }
    }
    //if(cov==m)
    cout<<(ans==INT_MAX?-1:ans)<<endl;
}
int main(){
    ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
	//cin>>t;
	while(t--){
        code();
	}
}


#include<cstdio>
#include<cstring>
const int MAX_N = 2e5 + 1;
int main() {
	int n, m, a[MAX_N], i, k[MAX_N], c[MAX_N] = {}, can = 1, l, r, mid, sumk = 0, cnt, v, baseCnt = 0;
	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		c[a[i]]++;
	}
	for (i = 1; i <= m; i++) {
		scanf("%d", &k[i]);
		sumk += k[i];
		baseCnt += !k[i];
	}
	for (i = 1; can && i <= m; i++)
		can &= c[i] >= k[i];
	if (!can) puts("-1");
	else {
		l = 1, r = n;
		while (l <= r) {
			mid = (l + r) >> 1;
			memset(c, 0, sizeof c);
			can = 0;
			cnt = baseCnt;
			for (i = 0; i < mid; i++) {
				v = a[i];
				c[v]++;
				if (c[v] == k[v]) cnt++;
			}
			can |= cnt == m;
			while (!can && i < n) {
				v = a[i - mid];
				c[v]--;
				if (c[v] < k[v]) cnt--;

				v = a[i];
				c[v]++;
				if (c[v] == k[v]) cnt++;

				can |= cnt == m;
				i++;
			}
			if (can) r = mid - 1;
			else l = mid + 1;
		}
		printf("%d", baseCnt == m ? 0 : l - sumk);
	}
	return 0;
};


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX_N = 2e5 + 1;
int main() {
	int n, m, a[MAX_N] = {}, i, k[MAX_N] = {}, c[MAX_N] = {}, sumk = 0, l, r, cnt = 0, ans = 1e9, v;
	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		scanf("%d", &a[i]);
	for (i = 1; i <= m; i++) {
		scanf("%d", &k[i]);
		sumk += k[i];
		cnt += !k[i];
	}
	l = r = 0;
	while (l < n) { // [l, r)
		while (cnt < m && r < n) {
			v = a[r];
			c[v]++;
			if (c[v] == k[v]) cnt++;
			r++;
		}
		if (cnt < m) break;
		ans = min(ans, r - l - sumk);
		v = a[l];
		if (c[v] == k[v]) cnt--;
		c[v]--;
		l++;
	}
	if (ans == 1e9) puts("-1");
	else printf("%d", ans);
	return 0;
};


#include<cstdio>
#include<cstring>
const int MAX_N = 2e5 + 1;
int main() {
	int n, m, a[MAX_N], i, k[MAX_N], c[MAX_N] = {}, can = 1, l, r, mid, sumk = 0, cnt, v, baseCnt = 0;
	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		c[a[i]]++;
	}
	for (i = 1; i <= m; i++) {
		scanf("%d", &k[i]);
		sumk += k[i];
		baseCnt += !k[i];
	}
	for (i = 1; can && i <= m; i++)
		can &= c[i] >= k[i];
	if (!can) puts("-1");
	else {
		l = 1, r = n;
		while (l <= r) {
			mid = (l + r) >> 1;
			memset(c, 0, sizeof c);
			can = 0;
			cnt = baseCnt;
			for (i = 0; i < mid; i++) {
				c[a[i]]++;
				if (c[a[i]] == k[a[i]])
					cnt++;
			}
			can |= cnt == m;
			while (!can && i < n) {
				v = a[i - mid];
				c[v]--;
				if (c[v] < k[v]) cnt--;

				v = a[i];
				c[v]++;
				if (c[v] == k[v]) cnt++;

				can |= cnt == m;
				i++;
			}
			if (can) r = mid - 1;
			else l = mid + 1;
		}
		printf("%d", l - sumk);
	}
	return 0;
};


#include<cstdio>
#include<cstring>
const int MAX_N = 2e5 + 1;
int main() {
	int n, m, a[MAX_N], i, k[MAX_N], c[MAX_N] = {}, can = 1, l, r, mid, sumk = 0, cnt, v;
	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		c[a[i]]++;
	}
	for (i = 1; i <= m; i++) {
		scanf("%d", &k[i]);
		sumk += k[i];
	}
	for (i = 1; can && i <= m; i++)
		can &= c[i] >= k[i];
	if (!can) puts("-1");
	else {
		l = sumk, r = n - 1;
		while (l <= r) {
			mid = (l + r) >> 1;
			memset(c, 0, sizeof c);
			can = cnt = 0;
			for (i = 0; i < mid; i++) {
				c[a[i]]++;
				if (c[a[i]] == k[a[i]])
					cnt++;
			}
			can |= cnt == m;
			while (!can && i < n) {
				v = a[i - mid];
				c[v]--;
				if (c[v] < k[v]) cnt--;

				v = a[i];
				c[v]++;
				if (c[v] == k[v]) cnt++;

				can |= cnt == m;
				i++;
			}
			if (can) r = mid - 1;
			else l = mid + 1;
		}
		printf("%d", l - sumk);
	}
	return 0;
};


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define R(i,b,a) for(int i=(int)(b);i>=(int)(a);i--)
#define endl "\n"
#define ios std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define out(x) cout<<x<<endl,exit(0);
#define pii pair<int,int>
#define pb push_back
#define all(v) v.begin(),v.end()
#define I first
#define S second

const int N=2e5+9;
int sum,a[N],req[N],n,m,bad;

int chk(int x){
    int cur[m+3]={0},bad=0;

    F(i,1,x) cur[a[i]]++;

    F(i,1,m){
        if(cur[i]<req[i]) bad++;
    }
    if(bad==0) return true;

    F(i,x+1,n){
        cur[a[i-x]]--;
        cur[a[i]]++;
        if(cur[a[i-x]]-req[a[i-x]]==-1) bad++;
        if(cur[a[i]]-req[a[i]]==0) bad--;

        if(bad==0) return true;
    }
    return false;
}

int32_t main(){
    ios;
    cin>>n>>m;

    F(i,1,n) cin>>a[i];
    F(i,1,m) cin>>req[i],sum+=req[i];

    int l=sum,r=n,mid;
    while(l<=r){
        mid=l+r>>1;
        if(chk(mid)) r=mid-1;
        else l=mid+1;
    }

    if(r==n) cout<<-1;
    else cout<<r+1-sum;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define R(i,b,a) for(int i=(int)(b);i>=(int)(a);i--)
#define endl "\n"
#define ios std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define out(x) cout<<x<<endl,exit(0);
#define pii pair<int,int>
#define pb push_back
#define all(v) v.begin(),v.end()
#define I first
#define S second

const int N=2e5+9;
int sum,a[N],req[N],fen[N],n,m,bad;

void update(int x,int del){
    while(x<=n){
        fen[x]+=del;
        x+=(x&-x);
    }
}

int query(int x){
    int sum=0;
    while(x>0){
        sum+=fen[x];
        x-=(x&-x);
    }
    return sum;
}

int chk(int x){
    int cur[m+3]={0},bad=0;
    memset(fen,0,sizeof fen);

    F(i,1,x) cur[a[i]]++;

    F(i,1,m){
        update(i,cur[i]-req[i]);
        if(cur[i]<req[i]) bad++;
    }
    if(bad==0 && query(m)==x-sum) return true;

    F(i,x+1,n){
        cur[a[i-x]]--;
        cur[a[i]]++;
        update(a[i-x],-1);
        if(cur[a[i-x]]-req[a[i-x]]==-1) bad++;
        update(a[i],1);
        if(cur[a[i]]-req[a[i]]==0) bad--;

        if(bad==0 && query(m)==x-sum) return true;
    }
    return false;
}

int32_t main(){
    ios;
    cin>>n>>m;

    F(i,1,n) cin>>a[i];
    F(i,1,m) cin>>req[i],sum+=req[i];

    int l=sum,r=n,mid;
    while(l<=r){
        mid=l+r>>1;
        if(chk(mid)) r=mid-1;
        else l=mid+1;
    }

    if(r==n) cout<<-1;
    else cout<<r+1-sum;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define R(i,b,a) for(int i=(int)(b);i>=(int)(a);i--)
#define endl "\n"
#define ios std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define out(x) cout<<x<<endl,exit(0);
#define pii pair<int,int>
#define pb push_back
#define all(v) v.begin(),v.end()
#define I first
#define S second

const int N=2e5+9;
int sum,a[N],req[N],fen[N],n,m,bad;

void update(int x,int del){
    while(x<=n){
        fen[x]+=del;
        x+=(x&-x);
    }
}

int query(int x){
    int sum=0;
    while(x>0){
        sum+=fen[x];
        x-=(x&-x);
    }
    return sum;
}

int chk(int x){
    int cur[m+3]={0},bad=0;
    memset(fen,0,sizeof fen);

    F(i,1,x) cur[a[i]]++;

    F(i,1,m){
        update(i,cur[i]-req[i]);
        if(cur[i]<req[i]) bad++;
    }
    if(bad==0 && query(m)==x-sum) return true;

    F(i,x+1,n){
        cur[a[i-x]]--;
        cur[a[i]]++;
        update(a[i-x],-1);
        if(cur[a[i-x]]-req[a[i-x]]==-1) bad++;
        update(a[i],1);
        if(cur[a[i]]-req[a[i]]==0) bad--;

        if(bad==0 && query(m)==x-sum) return true;
    }
    return false;
}

int32_t main(){
    ios;
    cin>>n>>m;

    F(i,1,n) cin>>a[i];
    F(i,1,m) cin>>req[i],sum+=req[i];

    int l=sum,r=n,mid;
    while(l<=r){
        mid=l+r>>1;
        if(chk(mid)) r=mid-1;
        else l=mid+1;
    }

    cout<<r+1-sum;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define R(i,b,a) for(int i=(int)(b);i>=(int)(a);i--)
#define endl "\n"
#define ios std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define out(x) cout<<x<<endl,exit(0);
#define pii pair<int,int>
#define pb push_back
#define all(v) v.begin(),v.end()
#define I first
#define S second

const int N=2e5+9;
int sum,a[N],req[N],n,m,bad;

int chk(int x){
    int cur[m+3]={0},bad=0;

    F(i,1,x) cur[a[i]]++;

    F(i,1,m){
        if(cur[i]<req[i]) bad++;
    }
    if(bad==0) return true;

    F(i,x+1,n){
        cur[a[i-x]]--;
        if(cur[a[i-x]]-req[a[i-x]]==-1) bad++;
        cur[a[i]]++;
        if(cur[a[i]]-req[a[i]]==0) bad--;

        if(bad==0) return true;
    }
    return false;
}

int32_t main(){
    ios;
    cin>>n>>m;

    F(i,1,n) cin>>a[i];
    F(i,1,m) cin>>req[i],sum+=req[i];

    int l=sum,r=n,mid;
    while(l<=r){
        mid=l+r>>1;
        if(chk(mid)) r=mid-1;
        else l=mid+1;
    }

    if(r==n) cout<<-1;
    else cout<<r+1-sum;
}


#include <bits/stdc++.h>
#define lli long long int
#define pb push_back
#define mod 1000000007
#pragma GCC optimize ("-O2")
#define mod2 998244353
#define MAXN 1000000000
#define v32 vector<int>
#define v64 vector<lli>
#define v1024 vector <vector <int>>
#define v4096 vector <vector <lli>>
#define vt vector
#define f(x, y, z) for (lli x = y; x < z; x++)
#define fd(x, y, z) for (lli x = y; x > z; x--)
#define lb lower_bound
#define ld long double
#define m64 map<lli,lli>
#define m32 map<int,int>
#define m64it map<lli,lli>::iterator
#define m32it map<int,int>::iterator
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ist insert
#define p_q priority_queue
#define min_p_q priority_queue <int,vt <int>,greater <int>>
using namespace std;

class UnionFind //rank is equal to number of vertices in a connected component
{
  public: v32 p, rank;
  // remember: vi is vector<int>
  UnionFind(int N) { rank.assign(N, 1);
  p.assign(N, 0); for (int i = 0; i < N; i++) p[i] = i; }
  int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
  bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
  void unionSet(int i, int j)
  {
  if (!isSameSet(i, j)) {
  // if from different set
  int x = findSet(i), y = findSet(j);
  if (rank[x] > rank[y]) p[y] = x,rank[x]+=rank[y];
  // rank keeps the tree short
  else
     {
     p[x] = y;
     rank[y]+=rank[x];
     }

  }

  }
};
class RMQ  //gives index of min-max in a given range
{
    public:
    struct node
    {
    int mn,mx,l,r;
    };
    v32 h;
    vt <node> st;

    bool is_intersection(int l,int r,int ll,int rr)
    {
       if(r<ll || rr<l)
         return 0;
         return 1;
    }
    RMQ(v32 a)
    {
       h=a;
       st.resize(4*h.size());
       build(0,h.size()-1);
    }
    void build(int l,int r,int k=0)
    {
        st[k].l=l,st[k].r=r;
        if(l==r)
           {st[k].mx=st[k].mn=l;
             return;
           }
        build(l,(l+r)/2,2*k+1);
        build((l+r)/2+1,r,2*k+2);
        if(h[st[2*k+1].mn]<=h[st[2*k+2].mn])
        st[k].mn=st[2*k+1].mn;
        else st[k].mn=st[2*k+2].mn;
        if(h[st[2*k+1].mx]>=h[st[2*k+2].mx])
        st[k].mx=st[2*k+1].mx;
        else st[k].mx=st[2*k+2].mx;
    }
    int minquery(int l,int r,int k=0)
    {
        if(l>r)return MAXN;
        int ll=st[k].l,rr=st[k].r,mid=(ll+rr)/2;
        if(ll>=l && rr<=r)
         return st[k].mn;
        int ans=l;
        if(!(r<ll || mid<l))
           ans=minquery(l,r,2*k+1);
        if(!(r<mid+1 || rr<l))
         {if(h[ans]>h[minquery(l,r,2*k+2)])
           ans=minquery(l,r,2*k+2);}
        return ans;
    }
    int maxquery(int l,int r,int k=0)
    {
       if(l>r)return -MAXN;
       int ll=st[k].l,rr=st[k].r,mid=(ll+rr)/2;
       if(ll>=l && rr<=r)
          return st[k].mx;
       int ans=l;
       if(!(r<ll || mid<l))
          ans=maxquery(l,r,2*k+1);
       if(!(r<mid+1 || rr<l))
          {if(h[ans]<h[maxquery(l,r,2*k+2)])
              ans=maxquery(l,r,2*k+2);}
       return ans;
    }
    void update(int id,int val,int k=0)
    {
        int l=st[k].l,r=st[k].r,mid=(l+r)/2;
        if(l==r)
        {
            h[l]=val;
            return;
        }
        if(id>=l && id<=mid)
        update(id,val,2*k+1);
        if(mid+1<= id && id<=r)
        update(id,val,2*k+2);
        if(h[st[2*k+1].mn]<=h[st[2*k+2].mn])
        st[k].mn=st[2*k+1].mn;
        else st[k].mn=st[2*k+2].mn;
        if(h[st[2*k+1].mx]>=h[st[2*k+2].mx])
        st[k].mx=st[2*k+1].mx;
        else st[k].mx=st[2*k+2].mx;
    }
};
class LAZY //currently set to set a given range by a value
{
   public:
   struct node
   {
    int l,r,lazy=0;lli lazyval=0;
    lli sum=0;
   };
   vt <node> st;v32 h;
   LAZY(v32 a)
   {
      h=a;
      st.resize(4*h.size());
      cst(0,h.size()-1);
   }
   int intersectiontype1(int l,int r,int ll,int rr)
   {
        if(r<ll || rr<l)
        return 0;
        return 1;
   }
   void cst(int l,int r,int k=0)
   {
    st[k].l=l,st[k].r=r;
    if(l==r)
        {
          st[k].sum=h[l];
          return;
        }
    cst(l,(l+r)/2,2*k+1);
    cst((l+r)/2+1,r,2*k+2);
    st[k].sum=st[2*k+1].sum+st[2*k+2].sum;
   }
   void shift(int k)
   {
   st[k].sum=(st[k].r-st[k].l+1)*st[k].lazyval;
   if(st[k].l!=st[k].r)
   {st[2*k+1].lazyval=st[k].lazyval;
   st[2*k+2].lazyval=st[k].lazyval;
   st[2*k+1].lazy=st[2*k+1].lazy=1;
   st[2*k+2].lazy=st[2*k+2].lazy=1;}
   st[k].lazyval=0;
   st[k].lazy=0;
   }
   lli query(int l,int r,int k=0)
   {
    if(st[k].lazy!=0)
       shift(k);
   int ll=st[k].l,rr=st[k].r,mid=(ll+rr)/2;
   if(ll>=l && rr<=r)
       return st[k].sum;
       lli ans=0;
       if(intersectiontype1(ll,mid,l,r)==1)
          ans=query(l,r,2*k+1);
       if(intersectiontype1(1+mid,rr,l,r)==1)
          ans+=query(l,r,2*k+2);
          return ans;
   }
   void update(int l,int r,lli x,int k=0)
   {
    if(l>r)
        return;
   int ll=st[k].l,rr=st[k].r,mid=(ll+rr)/2;
   if(ll>=l && rr<=r)
         {st[k].lazyval=x;
          st[k].lazy=1;
         return;}
         if(st[k].lazy!=0)
           shift(k);
           if(st[k].l==st[k].r) return;
   if(intersectiontype1(ll,mid,l,r)==1)
      update(l,r,x,2*k+1);
   if(intersectiontype1(mid+1,rr,l,r)==1)
      update(l,r,x,2*k+2);
      if(st[2*k+1].lazy!=0)
       shift(2*k+1);
       if(st[2*k+2].lazy!=0)
       shift(2*k+2);
    st[k].sum=st[2*k+1].sum+st[2*k+2].sum;
   }
   int lower_bound(int l,int r,int val)
   {
    while(l!=r)
    {
      int mid=(l+r)/2;
      if(query(mid,mid)>=val)
        r=mid;
      else
        {l=mid+1;
         if(mid+1<h.size())
             if(query(mid+1,mid+1)>val)
             {
              l=mid;
              break;
             }
        }
    }
    return l;
   }
};
class span
{
  public:
    v32 a;
    span()
    {};
    v32 max_left_border_without_equality()
     {
    int n=a.size();
    v32 ta,al(n);
    f(i,0,n)
    {
      if(ta.size()==0)
        {
            ta.pb(i);
            al[i]=i;
        }
      else
      {
        while(a[i]>a[ta[ta.size()-1]]) {ta.pop_back();if(ta.size()==0)break;}
         if(ta.size()==0)
         {
            ta.pb(i);
            al[i]=0;
         }
         else
         {
            al[i]=ta[ta.size()-1]+1;
            ta.pb(i);
         }
      }
    }
    return al;
     }
    v32 max_left_border_with_equality()
     {
    int n=a.size();
    v32 ta,al(n);
    f(i,0,n)
    {
      if(ta.size()==0)
        {
            ta.pb(i);
            al[i]=i;
        }
      else
      {
        while(a[i]>=a[ta[ta.size()-1]]) {ta.pop_back();if(ta.size()==0)break;}
         if(ta.size()==0)
         {
            ta.pb(i);
            al[i]=0;
         }
         else
         {
            al[i]=ta[ta.size()-1]+1;
            ta.pb(i);
         }
      }
    }
    return al;
     }
    v32 min_left_border_without_equality()
     {
    int n=a.size();
    v32 ta,al(n);
    f(i,0,n)
    {
      if(ta.size()==0)
        {
            ta.pb(i);
            al[i]=i;
        }
      else
      {
        while(a[i]<a[ta[ta.size()-1]]) {ta.pop_back();if(ta.size()==0)break;}
         if(ta.size()==0)
         {
            ta.pb(i);
            al[i]=0;
         }
         else
         {
            al[i]=ta[ta.size()-1]+1;
            ta.pb(i);
         }
      }
    }
    return al;
     }
    v32 min_left_border_with_equality()
     {
    int n=a.size();
    v32 ta,al(n);
    f(i,0,n)
    {
      if(ta.size()==0)
        {
            ta.pb(i);
            al[i]=i;
        }
      else
      {
        while(a[i]<=a[ta[ta.size()-1]]) {ta.pop_back();if(ta.size()==0)break;}
         if(ta.size()==0)
         {
            ta.pb(i);
            al[i]=0;
         }
         else
         {
            al[i]=ta[ta.size()-1]+1;
            ta.pb(i);
         }
      }
    }
    return al;
     }
    v32 max_right_border_without_equality()
     {
    int n=a.size();
    reverse(a.begin(),a.end());
    v32 ans=max_left_border_without_equality();
    reverse(ans.begin(),ans.end());
    f(i,0,n)ans[i]=n-1-ans[i];
    reverse(a.begin(),a.end());
    return ans;
     }
    v32 max_right_border_with_equality()
     {
    int n=a.size();
    reverse(a.begin(),a.end());
    v32 ans=max_left_border_with_equality();
    reverse(ans.begin(),ans.end());
    f(i,0,n)ans[i]=n-1-ans[i];
    reverse(a.begin(),a.end());
    return ans;
     }
    v32 min_right_border_without_equality()
     {
    int n=a.size();
    reverse(a.begin(),a.end());
    v32 ans=min_left_border_without_equality();
    reverse(ans.begin(),ans.end());
    f(i,0,n)ans[i]=n-1-ans[i];
    reverse(a.begin(),a.end());
    return ans;
     }
     v32 min_right_border_with_equality()
     {
    int n=a.size();
    reverse(a.begin(),a.end());
    v32 ans=min_left_border_with_equality();
    reverse(ans.begin(),ans.end());
    f(i,0,n)ans[i]=n-1-ans[i];
    reverse(a.begin(),a.end());
    return ans;
     }

};
class merge_sort_tree
{
  public:
    lli cnt(lli x,vector <int> &arr)
    {
     lli n=arr.size();
     vector<int>::iterator low,high;
     low = lower_bound(arr.begin(), arr.end(), x);
     if (low == (arr.end()) || *low != x)
     return 0;
     high = upper_bound(low, arr.end(), x);
     return high - low;
    }
    lli get_last_smaller(vector<int>& v, int x)
    {
     lli first = 0, last = v.size()-1;
     while (first <= last)
     {
       lli mid = (first + last) / 2;
       if (v[mid] >= x)
       last = mid - 1;
       else
       first = mid + 1;
     }
  return first - 1 < 0 ? -1 : first - 1;
    }
    struct node
    {
      int l,r;
      vector <int> arr;
    };
    v32 a;
    vt <node> st;

    merge_sort_tree(v32 b)
    {
      st.resize(4*b.size());
      a=b;
      build(0,b.size()-1);
    }
    void build(int l,int r,int k=0)
      {
        st[k].l=l,st[k].r=r;
        for(int i=l;i<r+1;i++)
        st[k].arr.push_back(a[i]);
        sort(st[k].arr.begin(),st[k].arr.end());
        if(l==r)
        return;
        build(l,(l+r)/2,2*k+1);
        build((l+r)/2+1,r,2*k+2);
      }
    bool intersection(int l,int r,int ll,int rr)
    {
      if(ll>r || l>rr)return 0;
      return 1;
    }
    lli count_val_in_range(int l,int r,int t,int k=0)
    {
     if(l>r)return 0;
     int ll=st[k].l,rr=st[k].r,mid=(ll+rr)/2;
     if(ll>=l && rr<=r)
     return cnt(t,st[k].arr);
     lli ans=0;
     if(intersection(l,r,ll,mid)==1)
     ans+=count_val_in_range(l,r,t,2*k+1);
     if(intersection(l,r,mid+1,rr)==1)
     ans+=count_val_in_range(l,r,t,2*k+2);
     return ans;
    }
    lli count_vals_less_than_given_val(int l,int r,int t,int k,vector<node> &st)
    {
     if(l>r)return 0;
     int ll=st[k].l,rr=st[k].r,mid=(ll+rr)/2;
     if(ll>=l && rr<=r)
     return get_last_smaller(st[k].arr,t)+1;
     lli ans=0;
     if(intersection(l,r,ll,mid)==1)
     ans+=count_vals_less_than_given_val(l,r,t,2*k+1,st);
     if(intersection(l,r,mid+1,rr)==1)
     ans+=count_vals_less_than_given_val(l,r,t,2*k+2,st);
     return ans;
    }
};
void DEBUG_ARR(v32 a)
{
    f(i,0,a.size())cout<<a[i]<<" ";
    cout<<"\n";
}
lli tmod(lli x,lli m){return (x%m+m)%m;}//USE AT YOUR OWN RISK
lli power(lli x, lli y)
{
    lli res = 1;

    while (y > 0)
    {
        if (y & 1)
            res = (res*x)%mod;
        y = y>>1;
        x = (x*x)%mod;
    }
    if(res<0)
       res+=mod;
    return res;
}
lli modInverse(lli a, lli m)
{
    lli m0 = m;
    lli y = 0, x = 1;
    if (m == 1)
      return 0;

    while (a > 1)
    {
        lli q = a / m;
        lli t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0)
       x += m0;
    return x;
}
int cnt[200001];
int smallest_correct_prefix(int l,int r,int ccnt[],int knight_color [],int m)
{
    int mid=(l+r)/2;
    f(i,1,m+1)cnt[i]=0;
    f(i,0,mid+1)cnt[knight_color[i]]++;
    if(l==r)return l;
    f(i,1,m+1)if(cnt[i]<ccnt[i])return smallest_correct_prefix(mid+1,r,ccnt,knight_color,m);
    return smallest_correct_prefix(l,mid,ccnt,knight_color,m);
}
int main()
{
  fastio;
  int n,m;cin>>n>>m;
  int knight_color[n];f(i,0,n)cin>>knight_color[i];
  int ccnt[m+1];f(i,0,m)cin>>ccnt[i+1];
  int sum=0;f(i,1,m+1)sum+=ccnt[i];
  int l=0,r;
  int ans=MAXN;
  f(i,0,n)cnt[knight_color[i]]++;
  f(i,1,m+1){
    if(cnt[i]<ccnt[i]){
        cout<<-1;
        return 0;
    }
  }
   r=smallest_correct_prefix(0,n-1,ccnt,knight_color,m);
  int bada_sum=0;
  f(i,1,m+1)bada_sum+=cnt[i];
  while(r!=n)
  {
     while(cnt[knight_color[l]]>=ccnt[knight_color[l]]+1){
        cnt[knight_color[l]]--;
        bada_sum--;
        l++;
     }
     ans=min(ans,bada_sum-sum);
     r++;if(r==n)break;
     cnt[knight_color[r]]++;
     bada_sum++;
  }
  cout<<ans;
  return 0;
}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<bool> vb;
#define pb push_back
#define fi first
#define se second
#define in insert
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9 + 7;
void solve(){
    int n, m;
    cin >> n >> m;
    vi a(n), k(m);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        a[i]--;
    }
    unordered_set<int> s;
    int cnt = 0;
    for (int i = 0; i < m; ++i) {
        cin >> k[i];
        if (k[i] > 0) s.in(i);
        cnt += k[i];
    }
    int l = 0;
    int r = 0;
    int ans = INT_MAX;
    while (r < n) {
        if (s.size() > 0) {
            k[a[r]]--;
            if (k[a[r]] > 0) s.in(a[r]);
            else s.erase(a[r]);
            ++r;
        } else {
            ans = min(ans, r - l - cnt);
            k[a[l]]++;
            if (k[a[r]] > 0) s.in(a[l]);
            else s.erase(a[l]);
            ++l;
        }
    }
    while (s.size() == 0 && l < n) {
        ans = min(ans, r - l - cnt);
        k[a[l]]++;
        if (k[a[r]] > 0) s.in(a[l]);
        else s.erase(a[l]);
        ++l;
    }
    if (ans == INT_MAX) cout << -1;
    else cout << ans;
}
int main(){
    speed;
    int t = 1;
    //cin >> t;
    for (int i = 1; i <= t; ++i) {
        solve();
    }
}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<bool> vb;
#define pb push_back
#define fi first
#define se second
#define in insert
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9 + 7;
void solve(){
    int n, m;
    cin >> n >> m;
    vi a(n), k(m);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        a[i]--;
    }
    unordered_set<int> s;
    int cnt = 0;
    for (int i = 0; i < m; ++i) {
        cin >> k[i];
        if (k[i] > 0) s.in(i);
        cnt += k[i];
    }
    int l = 0;
    int r = 0;
    int ans = INT_MAX;
    while (r < n) {
        if (s.size() > 0) {
            k[a[r]]--;
            if (k[a[r]] > 0) s.in(a[r]);
            else s.erase(a[r]);
            ++r;
        } else {
            ans = min(ans, r - l - cnt);
            k[a[l]]++;
            if (k[a[r]] > 0) s.in(a[l]);
            else s.erase(a[l]);
            ++l;
        }
    }
    if (s.size() == 0) ans = min(ans, r - l - cnt);
    if (ans == INT_MAX) cout << -1;
    else cout << ans;
}
int main(){
    speed;
    int t = 1;
    //cin >> t;
    for (int i = 1; i <= t; ++i) {
        solve();
    }
}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<bool> vb;
#define pb push_back
#define fi first
#define se second
#define in insert
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9 + 7;
void solve(){
    int n, m;
    cin >> n >> m;
    vi a(n), k(m);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        a[i]--;
    }
    unordered_set<int> s;
    int cnt = 0;
    for (int i = 0; i < m; ++i) {
        cin >> k[i];
        if (k[i] > 0) s.in(i);
        cnt += k[i];
    }
    int l = 0;
    int r = 0;
    int ans = INT_MAX;
    while (r < n && l < n) {
        if (s.size() > 0) {
            k[a[r]]--;
            if (k[a[r]] > 0) s.in(a[r]);
            else s.erase(a[r]);
            ++r;
        } else {
            ans = min(ans, r - l - cnt);
            k[a[l]]++;
            if (k[a[l]] > 0) s.in(a[l]);
            else s.erase(a[l]);
            ++l;
        }
    }
    while (s.size() == 0 && l < n) {
        ans = min(ans, r - l - cnt);
        k[a[l]]++;
        if (k[a[l]] > 0) s.in(a[l]);
        else s.erase(a[l]);
        ++l;
    }
    if (ans == INT_MAX) cout << -1;
    else cout << ans;
}
int main(){
    speed;
    int t = 1;
    //cin >> t;
    for (int i = 1; i <= t; ++i) {
        solve();
    }
}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<bool> vb;
#define pb push_back
#define fi first
#define se second
#define in insert
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9 + 7;
void solve(){
    int n, m;
    cin >> n >> m;
    vi a(n), k(m);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        a[i]--;
    }
    unordered_set<int> s;
    int cnt = 0;
    for (int i = 0; i < m; ++i) {
        cin >> k[i];
        if (k[i] > 0) s.in(i);
        cnt += k[i];
    }
    int l = 0;
    int r = 0;
    int ans = INT_MAX;
    while (r < n) {
        if (s.size() > 0) {
            k[a[r]]--;
            if (k[a[r]] > 0) s.in(a[r]);
            else s.erase(a[r]);
            ++r;
        } else {
            ans = min(ans, r - l - cnt);
            k[a[l]]++;
            if (k[a[r]] > 0) s.in(a[l]);
            else s.erase(a[l]);
            ++l;
        }
    }
    while (s.size() == 0 && l < n) {
        ans = min(ans, r - l - cnt);
        k[a[l]]++;
        if (k[a[l]] > 0) s.in(a[l]);
        else s.erase(a[l]);
        ++l;
    }
    if (ans == INT_MAX) cout << -1;
    else cout << ans;
}
int main(){
    speed;
    int t = 1;
    //cin >> t;
    for (int i = 1; i <= t; ++i) {
        solve();
    }
}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<bool> vb;
#define pb push_back
#define fi first
#define se second
#define in insert
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9 + 7;
void solve(){
    int n, m;
    cin >> n >> m;
    vi a(n), k(m);
    for (int i = 0; i < n; ++i) cin >> a[i];
    int s = 0;
    for (int i = 0; i < m; ++i) {
        cin >> k[i];
        s += k[i];
    }
    vi clr(m, 0);
    for (int i = 0; i < n; ++i) {
        clr[a[i] - 1]++;
        if (i >= s - 1) {
            bool q = true;
            for (int j = 0; j < m; ++j) {
                if (clr[j] != k[j]) {
                    q = false;
                    break;
                }
            }
            if (q) {
                cout << "YES";
                return;
            }
            clr[a[i - s + 1] - 1]--;
        }
    }
    cout << "NO";
}
int main(){
    speed;
    int t = 1;
    //cin >> t;
    for (int i = 1; i <= t; ++i) {
        solve();
    }
}


#include <cstdlib>
#include <csignal>
#include <csetjmp>
#include <cstdarg>
#include <typeinfo>
#include <typeindex>
#include <type_traits>
#include <bitset>
#include <functional>
#include <utility>
#include <ctime>
#include <chrono>
#include <cstddef>
#include <initializer_list>
#include <tuple>
#include <any>
#include <optional>
#include <variant>
#include <new>
#include <memory>
#include <scoped_allocator>
#include <climits>
#include <cfloat>
#include <cstdint>
#include <cinttypes>
#include <limits>
#include <exception>
#include <stdexcept>
#include <cassert>
#include <system_error>
#include <cerrno>
#include <cctype>
#include <cwctype>
#include <cstring>
#include <cwchar>
#include <string>
#include <string_view>
#include <array>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <iterator>
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>
#include <numeric>
#include <iosfwd>
#include <ios>
#include <istream>
#include <ostream>
#include <iostream>
#include <fstream>
#include <sstream>
#include <strstream>
#include <iomanip>
#include <streambuf>
#include <cstdio>
#include <locale>
#include <clocale>
#include <codecvt>
#include <regex>
#include <atomic>
#include <thread>
#include <mutex>
#include <shared_mutex>
#include <future>
#include <condition_variable>

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define nfor(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define fore(i, l, r) for (int i = int(l); i < int(r); i++)
#define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
#define all(a) (a).begin(), (a).end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
#define mp(x, y) make_pair((x), (y))
#define x first
#define y second

using namespace std;

typedef long long li;
typedef long double ld;
typedef pair<int, int> pti;

template<typename X> inline X abs(const X& a) { return a < 0? -a: a; }
template<typename X> inline X sqr(const X& a) { return a * a; }

template<typename A, typename B> inline ostream& operator<< (ostream& out, const pair<A, B>& p) { return out << "(" << p.x << ", " << p.y << ")"; }
template<typename T> inline ostream& operator<< (ostream& out, const vector<T>& a) { out << "["; forn(i, sz(a)) { if (i) out << ','; out << ' ' << a[i]; } return out << " ]"; }
template<typename T> inline ostream& operator<< (ostream& out, const set<T>& a) { return out << vector<T>(all(a)); }
template<typename X, typename Y> inline ostream& operator<< (ostream& out, const map<X, Y>& a) { return out << vector<pair<X, Y>>(all(a)); }
template<typename T> inline ostream& operator<< (ostream& out, const pair<T*, int>& a) { return out << vector<T>(a.x, a.x + a.y); }

inline ld gett() { return ld(clock()) / CLOCKS_PER_SEC; }

const int INF = int(1e9);
const li INF64 = li(1e18);
const ld EPS = 1e-9, PI = 3.1415926535897932384626433832795;

#ifdef SU1
//#define GEN
#ifndef GEN
#define LOG
#endif
#endif

const int N = 200200;

int n, m;
int a[N], b[N];

bool read() {
  if (!(cin >> n >> m)) return false;
  forn(i, n) assert(scanf("%d", &a[i]) == 1), a[i]--;
  forn(i, m) assert(scanf("%d", &b[i]) == 1);
  return true;
}

int ds;
int c[N];

void add(int x, int dd) {
  if (c[x] >= b[x]) ds--;
  c[x] += dd;
  if (c[x] >= b[x]) ds++;
}

void solve() {
  memset(c, 0, sizeof(c));
  ds = 0;

  forn(i, m) if (b[i] == 0) ds++;

  int s = accumulate(b, b + m, 0);

  int ans = INF;

  int j = 0;
  forn(i, n) {
    assert(j >= i);
    while (j < n && ds < m) {
      add(a[j], +1);
      j++;
    }
    int cur = (j - i) - s;
#ifdef LOG
    clog << "i: " << i << " j: " << j << " cur: " << cur << endl;
    clog << "ds: " << ds << " c: " << mp(c, m) << " b: " << mp(b, m) << endl;
#endif
    if (ds < m) break;
    ans = min(ans, cur);
    add(a[i], -1);
  }

  if (ans == INF) ans = -1;

  cout << ans << endl;
}

int main() {
#ifdef SU1
  assert(freopen("input.txt", "rt", stdin));
  //assert(freopen("output.txt", "wt", stdout));
#endif

  cout << setprecision(10) << fixed;
  cerr << setprecision(5) << fixed;

  while (read()) {
    ld stime = gett();
    solve();
#ifdef SU1
    cerr << "Time: " << gett() - stime << endl;
#endif
    // break;
  }

  return 0;
}


#include <cstdlib>
#include <csignal>
#include <csetjmp>
#include <cstdarg>
#include <typeinfo>
#include <typeindex>
#include <type_traits>
#include <bitset>
#include <functional>
#include <utility>
#include <ctime>
#include <chrono>
#include <cstddef>
#include <initializer_list>
#include <tuple>
#include <any>
#include <optional>
#include <variant>
#include <new>
#include <memory>
#include <scoped_allocator>
#include <climits>
#include <cfloat>
#include <cstdint>
#include <cinttypes>
#include <limits>
#include <exception>
#include <stdexcept>
#include <cassert>
#include <system_error>
#include <cerrno>
#include <cctype>
#include <cwctype>
#include <cstring>
#include <cwchar>
#include <string>
#include <string_view>
#include <array>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <iterator>
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>
#include <numeric>
#include <iosfwd>
#include <ios>
#include <istream>
#include <ostream>
#include <iostream>
#include <fstream>
#include <sstream>
#include <strstream>
#include <iomanip>
#include <streambuf>
#include <cstdio>
#include <locale>
#include <clocale>
#include <codecvt>
#include <regex>
#include <atomic>
#include <thread>
#include <mutex>
#include <shared_mutex>
#include <future>
#include <condition_variable>

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define nfor(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define fore(i, l, r) for (int i = int(l); i < int(r); i++)
#define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
#define all(a) (a).begin(), (a).end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
#define mp(x, y) make_pair((x), (y))
#define x first
#define y second

using namespace std;

typedef long long li;
typedef long double ld;
typedef pair<int, int> pti;

template<typename X> inline X abs(const X& a) { return a < 0? -a: a; }
template<typename X> inline X sqr(const X& a) { return a * a; }

template<typename A, typename B> inline ostream& operator<< (ostream& out, const pair<A, B>& p) { return out << "(" << p.x << ", " << p.y << ")"; }
template<typename T> inline ostream& operator<< (ostream& out, const vector<T>& a) { out << "["; forn(i, sz(a)) { if (i) out << ','; out << ' ' << a[i]; } return out << " ]"; }
template<typename T> inline ostream& operator<< (ostream& out, const set<T>& a) { return out << vector<T>(all(a)); }
template<typename X, typename Y> inline ostream& operator<< (ostream& out, const map<X, Y>& a) { return out << vector<pair<X, Y>>(all(a)); }
template<typename T> inline ostream& operator<< (ostream& out, const pair<T*, int>& a) { return out << vector<T>(a.x, a.x + a.y); }

inline ld gett() { return ld(clock()) / CLOCKS_PER_SEC; }

const int INF = int(1e9);
const li INF64 = li(1e18);
const ld EPS = 1e-9, PI = 3.1415926535897932384626433832795;

#ifdef SU1
//#define GEN
#ifndef GEN
#define LOG
#endif
#endif

const int N = 200200;

int n, m;
int a[N], b[N];

bool read() {
  if (!(cin >> n >> m)) return false;
  forn(i, n) assert(scanf("%d", &a[i]) == 1), a[i]--;
  forn(i, m) assert(scanf("%d", &b[i]) == 1);
  return true;
}

int ds;
int c[N];

void add(int x, int dd) {
  if (c[x] >= b[x]) ds--;
  c[x] += dd;
  if (c[x] >= b[x]) ds++;
}

void solve() {
  memset(c, 0, sizeof(c));
  ds = 0;

  int s = accumulate(b, b + m, 0);

  int ans = INF;

  int j = 0;
  forn(i, n) {
    assert(j >= i);
    while (j < n && ds < m) {
      add(a[j], +1);
      j++;
    }
    int cur = (j - i) - s;
#ifdef LOG
    clog << "i: " << i << " j: " << j << " cur: " << cur << endl;
    clog << "ds: " << ds << " c: " << mp(c, m) << " b: " << mp(b, m) << endl;
#endif
    if (ds < m) break;
    ans = min(ans, cur);
    add(a[i], -1);
  }

  if (ans == INF) ans = -1;

  cout << ans << endl;
}

int main() {
#ifdef SU1
  assert(freopen("input.txt", "rt", stdin));
  //assert(freopen("output.txt", "wt", stdout));
#endif

  cout << setprecision(10) << fixed;
  cerr << setprecision(5) << fixed;

  while (read()) {
    ld stime = gett();
    solve();
#ifdef SU1
    cerr << "Time: " << gett() - stime << endl;
#endif
    //break;
  }

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll int
#define lli long long
#define pll pair<ll,ll>
#define min(x,y) ((x)<(y)?(x):(y))
#define max(x,y) ((x)>(y)?(x):(y))
#define abs(x) ((x)<0?(-(x)):(x))

const int NN = (int)2e5+5;
const int MOD = 998244353;

int a[NN];
int n,k;
int len_k[NN];
vector<int> p[NN];
int in_vec[NN];
int R[NN];

int main() {
	ios_base::sync_with_stdio(0);

	scanf("%d%d", &n, &k);
	for(int i = 0; i < n; i ++) {
		scanf("%d", &a[i]);
		a[i] --;
		in_vec[i] = p[a[i]].size();
		p[a[i]].push_back(i);
	}
	int req = 0;
	for(int i = 0; i < k; i ++) scanf("%d", &len_k[i]), req += len_k[i];

	memset(R, -1, sizeof(R));
	int ans = n + 1;
	set<int> s;
	for(int i = n - 1; i >= 0; i --) {
		int idx_in_vec = in_vec[i];
		int sz_vec = (int)p[a[i]].size();
		if(idx_in_vec + len_k[a[i]] <= sz_vec) {
			if(R[a[i]] != -1) {
				s.erase(R[a[i]]);
			}
			R[a[i]] = p[a[i]][idx_in_vec + len_k[a[i]] - 1];
			s.insert(R[a[i]]);
		}
		if(s.size() == k) {
			int mx_idx = *(--s.end());
			ans = min(ans, mx_idx - i + 1 - req);
		}
	}
	printf("%d\n", ans == n + 1? -1: ans);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
//#include <string>
//#include <queue>
//#include <stack>
//#include <map>
#include <vector>
//#include <memory>
#include <stdio.h>

#define ENDL putchar('\n')
#define SPACE putchar(' ')

bool io_wasDot = false;
int io_lastintsize = 0;
long long readint()
{
	char x = getchar(); bool neg = false; long long result = 0; io_lastintsize = 0;
	while(!isdigit(x) and x != '-') x = getchar();
	result = (x == '-') ? (neg = true)*0 : (x - '0' + (++io_lastintsize)*0);
	while(isdigit((x = getchar())))result = result*10 + x - '0', ++io_lastintsize; io_wasDot = (x == '.');
	return neg ? -result : result;
}
double readfloat()
{
	double res = readint();
	if(io_wasDot) {double tmp = readint(); res += tmp/std::pow(10, io_lastintsize);}
	return res;
}
void readstr(char *str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { *str = c; ++str; c=getchar(); } }
void readstr(std::vector<char> &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }
void readstr(std::string &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }

void printint(long long x){ if(x < 0) { putchar('-'); printint(-x); return; } if(x > 9) printint(x/10);  putchar('0'+x%10); }

void printstr(char *str){ for(char *cur = str; *cur; ++cur) putchar(*cur); }
void printstr(std::string &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }
void printstr(std::vector<char> &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }

int n, m;
std::vector< std::vector<int> > gp(0);
std::vector<int> cats(0);
int count = 0;
void dfs(int x, int xcats=0)
{
	if(gp[x].size() != 0)
	{
		for(int i = gp[x].size(); i != 0; --i)
		{
		    int val = gp[x][i-1];
		    if(!cats[val]) dfs(val);
		    else if(xcats+1 <= m) dfs(val, xcats+1);
		}
	}
	else
		++count;

}
main()
{
	n = readint(), m = readint();
	{
		std::vector< std::vector<int> > _gp(n, std::vector<int>(0));
		std::vector<int> _cats(n);
		gp.swap(_gp); cats.swap(_cats);
	}

	for(int i = 0; i < n; ++i)
		cats[i] = readint();
	for(int i = 0; i < n-1; ++i)
	{
		int x = readint(), y = readint();
		gp[x-1].push_back(y-1);
	}
	dfs(0,cats[0]);
	printint(count);
}


#include <iostream>
#include <algorithm>
#include <cmath>
//#include <string>
//#include <queue>
//#include <stack>
//#include <map>
#include <vector>
//#include <memory>
#include <stdio.h>

#define ENDL putchar('\n')
#define SPACE putchar(' ')

bool io_wasDot = false;
int io_lastintsize = 0;
long long readint()
{
	char x = getchar(); bool neg = false; long long result = 0; io_lastintsize = 0;
	while(!isdigit(x) and x != '-') x = getchar();
	result = (x == '-') ? (neg = true)*0 : (x - '0' + (++io_lastintsize)*0);
	while(isdigit((x = getchar())))result = result*10 + x - '0', ++io_lastintsize; io_wasDot = (x == '.');
	return neg ? -result : result;
}
double readfloat()
{
	double res = readint();
	if(io_wasDot) {double tmp = readint(); res += tmp/std::pow(10, io_lastintsize);}
	return res;
}
void readstr(char *str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { *str = c; ++str; c=getchar(); } }
void readstr(std::vector<char> &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }
void readstr(std::string &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }

void printint(long long x){ if(x < 0) { putchar('-'); printint(-x); return; } if(x > 9) printint(x/10);  putchar('0'+x%10); }

void printstr(char *str){ for(char *cur = str; *cur; ++cur) putchar(*cur); }
void printstr(std::string &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }
void printstr(std::vector<char> &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }

int n, m;
std::vector< std::vector<int> > gp(0);
std::vector<int> cats(0);
int count = 0;
void dfs(int x, int xcats=0)
{
	if(gp[x].size() != 0)
	{
		for(std::vector<int>::iterator a = gp[x].begin(), end = gp[x].end(); a != end; ++a)
			if(xcats + cats[*a] < m) dfs(*a, cats[*a] ? xcats + cats[*a] : 0);
	}
	else
	{
		++count;
		return;
	}
}
main()
{
	n = readint(), m = readint();
	{
		std::vector< std::vector<int> > _gp(n, std::vector<int>(0));
		std::vector<int> _cats(n);
		gp.swap(_gp); cats.swap(_cats);
	}

	for(int i = 0; i < n; ++i)
		cats[i] = readint();
	for(int i = 0; i < n-1; ++i)
	{
		int x = readint(), y = readint();
		gp[x-1].push_back(y-1);
	}
	dfs(0);
	printint(count);
}


#include <iostream>
#include <algorithm>
#include <cmath>
//#include <string>
//#include <queue>
//#include <stack>
//#include <map>
#include <vector>
//#include <memory>
#include <stdio.h>

#define ENDL putchar('\n')
#define SPACE putchar(' ')

bool io_wasDot = false;
int io_lastintsize = 0;
long long readint()
{
	char x = getchar(); bool neg = false; long long result = 0; io_lastintsize = 0;
	while(!isdigit(x) and x != '-') x = getchar();
	result = (x == '-') ? (neg = true)*0 : (x - '0' + (++io_lastintsize)*0);
	while(isdigit((x = getchar())))result = result*10 + x - '0', ++io_lastintsize; io_wasDot = (x == '.');
	return neg ? -result : result;
}
double readfloat()
{
	double res = readint();
	if(io_wasDot) {double tmp = readint(); res += tmp/std::pow(10, io_lastintsize);}
	return res;
}
void readstr(char *str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { *str = c; ++str; c=getchar(); } }
void readstr(std::vector<char> &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }
void readstr(std::string &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }

void printint(long long x){ if(x < 0) { putchar('-'); printint(-x); return; } if(x > 9) printint(x/10);  putchar('0'+x%10); }

void printstr(char *str){ for(char *cur = str; *cur; ++cur) putchar(*cur); }
void printstr(std::string &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }
void printstr(std::vector<char> &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }

int n, m;
std::vector< std::vector<int> > gp(0);
std::vector<int> cats(0);
int count = 0;
void dfs(int x, int xcats=0)
{
	if(x == n-1)
	{
		++count;
		return;
	}
	if(gp[x].size() != 0)
	for(std::vector<int>::iterator a = gp[x].begin(), end = gp[x].end(); a != end; ++a)
	{
		std::cout << *a << std::endl;
		if(xcats + cats[*a] <= m) dfs(*a, xcats+cats[*a]);
	}
}
main()
{
	n = readint(), m = readint();
	{
		std::vector< std::vector<int> > _gp(n, std::vector<int>(0));
		std::vector<int> _cats(n);
		gp.swap(_gp); cats.swap(_cats);
	}

	for(int i = 0; i < n; ++i)
		cats[i] = readint();
	for(int i = 0; i < n-1; ++i)
	{
		int x = readint(), y = readint();
		gp[x-1].push_back(y-1);
	}
	dfs(0);
	printint(count);
}


#include <iostream>
#include <algorithm>
#include <cmath>
//#include <string>
//#include <queue>
//#include <stack>
//#include <map>
#include <vector>
//#include <memory>
#include <stdio.h>

#define ENDL putchar('\n')
#define SPACE putchar(' ')

bool io_wasDot = false;
int io_lastintsize = 0;
long long readint()
{
	char x = getchar(); bool neg = false; long long result = 0; io_lastintsize = 0;
	while(!isdigit(x) and x != '-') x = getchar();
	result = (x == '-') ? (neg = true)*0 : (x - '0' + (++io_lastintsize)*0);
	while(isdigit((x = getchar())))result = result*10 + x - '0', ++io_lastintsize; io_wasDot = (x == '.');
	return neg ? -result : result;
}
double readfloat()
{
	double res = readint();
	if(io_wasDot) {double tmp = readint(); res += tmp/std::pow(10, io_lastintsize);}
	return res;
}
void readstr(char *str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { *str = c; ++str; c=getchar(); } }
void readstr(std::vector<char> &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }
void readstr(std::string &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }

void printint(long long x){ if(x < 0) { putchar('-'); printint(-x); return; } if(x > 9) printint(x/10);  putchar('0'+x%10); }

void printstr(char *str){ for(char *cur = str; *cur; ++cur) putchar(*cur); }
void printstr(std::string &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }
void printstr(std::vector<char> &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }

int n, m;
std::vector< std::vector<int> > gp(0);
std::vector<int> cats(0);
int count = 0;
void dfs(int x, int xcats=0)
{
	if(gp[x].size() != 0)
	{
		for(std::vector<int>::iterator a = gp[x].begin(), end = gp[x].end(); a != end; ++a)
			if(xcats + cats[*a] < m) dfs(*a, xcats+cats[*a]);
	}
	else
	{
		++count;
		return;
	}
}
main()
{
	n = readint(), m = readint();
	{
		std::vector< std::vector<int> > _gp(n, std::vector<int>(0));
		std::vector<int> _cats(n);
		gp.swap(_gp); cats.swap(_cats);
	}

	for(int i = 0; i < n; ++i)
		cats[i] = readint();
	for(int i = 0; i < n-1; ++i)
	{
		int x = readint(), y = readint();
		gp[x-1].push_back(y-1);
	}
	dfs(0);
	printint(count);
}


#include <iostream>
#include <algorithm>
#include <cmath>
//#include <string>
//#include <queue>
//#include <stack>
//#include <map>
#include <vector>
//#include <memory>
#include <stdio.h>

#define ENDL putchar('\n')
#define SPACE putchar(' ')

bool io_wasDot = false;
int io_lastintsize = 0;
long long readint()
{
	char x = getchar(); bool neg = false; long long result = 0; io_lastintsize = 0;
	while(!isdigit(x) and x != '-') x = getchar();
	result = (x == '-') ? (neg = true)*0 : (x - '0' + (++io_lastintsize)*0);
	while(isdigit((x = getchar())))result = result*10 + x - '0', ++io_lastintsize; io_wasDot = (x == '.');
	return neg ? -result : result;
}
double readfloat()
{
	double res = readint();
	if(io_wasDot) {double tmp = readint(); res += tmp/std::pow(10, io_lastintsize);}
	return res;
}
void readstr(char *str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { *str = c; ++str; c=getchar(); } }
void readstr(std::vector<char> &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }
void readstr(std::string &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }

void printint(long long x){ if(x < 0) { putchar('-'); printint(-x); return; } if(x > 9) printint(x/10);  putchar('0'+x%10); }

void printstr(char *str){ for(char *cur = str; *cur; ++cur) putchar(*cur); }
void printstr(std::string &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }
void printstr(std::vector<char> &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }

int n, m;
std::vector< std::vector<int> > gp(0);
std::vector<bool> cats(0);
int count = 0;
void dfs(int x, int from, int xcats=0)
{
	bool allvisited = 1;
	for(int i = gp[x].size(); i != 0; --i)
	{
        int val = gp[x][i-1];
	    if(val == from) continue;
	    allvisited = 0;
	    if(!cats[val]) dfs(val, x);
	    else if(xcats+1 <= m) dfs(val, x, xcats+1);
	}
    if(allvisited) ++count;
}
main()
{
	n = readint(), m = readint();
	int leafs = n-1;
	gp=std::vector< std::vector<int> >(n*2, std::vector<int>(0));
	cats=std::vector<bool>(n);

	for(int i = 0; i < n; ++i)
		cats[i] = readint();
	for(int i = 0; i < n-1; ++i)
	{
		int x = readint(), y = readint();
		gp[x-1].push_back(y-1); gp[y-1].push_back(x-1);
	}
	dfs(0, -1, cats[0]);
	printint(count);
}


#include <iostream>
#include <algorithm>
#include <cmath>
//#include <string>
//#include <queue>
//#include <stack>
//#include <map>
#include <vector>
//#include <memory>
#include <stdio.h>

#define ENDL putchar('\n')
#define SPACE putchar(' ')

bool io_wasDot = false;
int io_lastintsize = 0;
long long readint()
{
	char x = getchar(); bool neg = false; long long result = 0; io_lastintsize = 0;
	while(!isdigit(x) and x != '-') x = getchar();
	result = (x == '-') ? (neg = true)*0 : (x - '0' + (++io_lastintsize)*0);
	while(isdigit((x = getchar())))result = result*10 + x - '0', ++io_lastintsize; io_wasDot = (x == '.');
	return neg ? -result : result;
}
double readfloat()
{
	double res = readint();
	if(io_wasDot) {double tmp = readint(); res += tmp/std::pow(10, io_lastintsize);}
	return res;
}
void readstr(char *str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { *str = c; ++str; c=getchar(); } }
void readstr(std::vector<char> &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }
void readstr(std::string &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }

void printint(long long x){ if(x < 0) { putchar('-'); printint(-x); return; } if(x > 9) printint(x/10);  putchar('0'+x%10); }

void printstr(char *str){ for(char *cur = str; *cur; ++cur) putchar(*cur); }
void printstr(std::string &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }
void printstr(std::vector<char> &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }

int n, m;
std::vector< std::vector<int> > gp(0);
std::vector<int> cats(0);
int count = 0;
void dfs(int x, int xcats=0)
{
	if(gp[x].size() != 0)
	{
		for(int i = gp[x].size(); i != 0; --i)
		{
		    int val = gp[x][i-1];
		    if(!cats[i-1]) dfs(val);
		    else if(xcats+1 <= m) dfs(val, xcats+1);
		}
	}
	else
	{
		++count;
		return;
	}
}
main()
{
	n = readint(), m = readint();
	{
		std::vector< std::vector<int> > _gp(n, std::vector<int>(0));
		std::vector<int> _cats(n);
		gp.swap(_gp); cats.swap(_cats);
	}

	for(int i = 0; i < n; ++i)
		cats[i] = readint();
	for(int i = 0; i < n-1; ++i)
	{
		int x = readint(), y = readint();
		gp[x-1].push_back(y-1);
	}
	dfs(0,cats[0]);
	printint(count);
}


#include <iostream>
#include <algorithm>
#include <cmath>
//#include <string>
//#include <queue>
//#include <stack>
//#include <map>
#include <vector>
//#include <memory>
#include <stdio.h>

#define ENDL putchar('\n')
#define SPACE putchar(' ')

bool io_wasDot = false;
int io_lastintsize = 0;
long long readint()
{
	char x = getchar(); bool neg = false; long long result = 0; io_lastintsize = 0;
	while(!isdigit(x) and x != '-') x = getchar();
	result = (x == '-') ? (neg = true)*0 : (x - '0' + (++io_lastintsize)*0);
	while(isdigit((x = getchar())))result = result*10 + x - '0', ++io_lastintsize; io_wasDot = (x == '.');
	return neg ? -result : result;
}
double readfloat()
{
	double res = readint();
	if(io_wasDot) {double tmp = readint(); res += tmp/std::pow(10, io_lastintsize);}
	return res;
}
void readstr(char *str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { *str = c; ++str; c=getchar(); } }
void readstr(std::vector<char> &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }
void readstr(std::string &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }

void printint(long long x){ if(x < 0) { putchar('-'); printint(-x); return; } if(x > 9) printint(x/10);  putchar('0'+x%10); }

void printstr(char *str){ for(char *cur = str; *cur; ++cur) putchar(*cur); }
void printstr(std::string &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }
void printstr(std::vector<char> &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }

int n, m;
std::vector< std::vector<int> > gp(0);
std::vector<bool> cats(0);
int count = 0;
void dfs(int x, std::vector<bool> visited, int xcats=0)
{
    visited[x] = true;
	bool allvisited = 0;
	for(int i = gp[x].size(); i != 0; --i)
	{
        int val = gp[x][i-1];
	    if(!visited[val])
	    {
	        allvisited = 1;
	        if(!cats[val]) dfs(val, visited);
	        else if(xcats+1 <= m) dfs(val, visited, xcats+1);
	    }
	}
    if(!allvisited)
		++count;
}
main()
{
	n = readint(), m = readint();

	gp=std::vector< std::vector<int> >(n, std::vector<int>(0));
	cats=std::vector<bool>(n);

	for(int i = 0; i < n; ++i)
		cats[i] = readint();
	for(int i = 0; i < n-1; ++i)
	{
		int x = readint(), y = readint();
		gp[x-1].push_back(y-1); gp[y-1].push_back(x-1);
	}
	dfs(0, std::vector<bool>(n,0), cats[0]);
	printint(count);
}


#include <iostream>
#include <algorithm>
#include <cmath>
//#include <string>
//#include <queue>
//#include <stack>
//#include <map>
#include <vector>
//#include <memory>
#include <stdio.h>

#define ENDL putchar('\n')
#define SPACE putchar(' ')

bool io_wasDot = false;
int io_lastintsize = 0;
long long readint()
{
	char x = getchar(); bool neg = false; long long result = 0; io_lastintsize = 0;
	while(!isdigit(x) and x != '-') x = getchar();
	result = (x == '-') ? (neg = true)*0 : (x - '0' + (++io_lastintsize)*0);
	while(isdigit((x = getchar())))result = result*10 + x - '0', ++io_lastintsize; io_wasDot = (x == '.');
	return neg ? -result : result;
}
double readfloat()
{
	double res = readint();
	if(io_wasDot) {double tmp = readint(); res += tmp/std::pow(10, io_lastintsize);}
	return res;
}
void readstr(char *str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { *str = c; ++str; c=getchar(); } }
void readstr(std::vector<char> &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }
void readstr(std::string &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }

void printint(long long x){ if(x < 0) { putchar('-'); printint(-x); return; } if(x > 9) printint(x/10);  putchar('0'+x%10); }

void printstr(char *str){ for(char *cur = str; *cur; ++cur) putchar(*cur); }
void printstr(std::string &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }
void printstr(std::vector<char> &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }

int n, m;
std::vector< std::vector<int> > gp(0);
std::vector<int> cats(0);
int count = 0;
void dfs(int x, int xcats=0)
{
	if(x == n-1)
	{
		++count;
		return;
	}
	if(gp[x].size() != 0)
	for(std::vector<int>::iterator a = gp[x].begin(), end = gp[x].end(); a != end; ++a)
		if(xcats + cats[*a] <= m) dfs(*a, xcats+cats[*a]);
}
main()
{
	n = readint(), m = readint();
	{
		std::vector< std::vector<int> > _gp(n, std::vector<int>(0));
		std::vector<int> _cats(n);
		gp.swap(_gp); cats.swap(_cats);
	}

	for(int i = 0; i < n; ++i)
		cats[i] = readint();
	for(int i = 0; i < n-1; ++i)
	{
		int x = readint(), y = readint();
		gp[x-1].push_back(y-1);
	}
	dfs(0);
	printint(count);
}


#include <iostream>
#include <algorithm>
#include <cmath>
//#include <string>
//#include <queue>
//#include <stack>
//#include <map>
#include <vector>
//#include <memory>
#include <stdio.h>

#define ENDL putchar('\n')
#define SPACE putchar(' ')

bool io_wasDot = false;
int io_lastintsize = 0;
long long readint()
{
	char x = getchar(); bool neg = false; long long result = 0; io_lastintsize = 0;
	while(!isdigit(x) and x != '-') x = getchar();
	result = (x == '-') ? (neg = true)*0 : (x - '0' + (++io_lastintsize)*0);
	while(isdigit((x = getchar())))result = result*10 + x - '0', ++io_lastintsize; io_wasDot = (x == '.');
	return neg ? -result : result;
}
double readfloat()
{
	double res = readint();
	if(io_wasDot) {double tmp = readint(); res += tmp/std::pow(10, io_lastintsize);}
	return res;
}
void readstr(char *str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { *str = c; ++str; c=getchar(); } }
void readstr(std::vector<char> &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }
void readstr(std::string &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }

void printint(long long x){ if(x < 0) { putchar('-'); printint(-x); return; } if(x > 9) printint(x/10);  putchar('0'+x%10); }

void printstr(char *str){ for(char *cur = str; *cur; ++cur) putchar(*cur); }
void printstr(std::string &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }
void printstr(std::vector<char> &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }

int n, m;
std::vector< std::vector<int> > gp(0);
std::vector<bool> cats(0);
int count = 0;
void dfs(int x, int xcats=0)
{
	bool allvisited = 1;
	for(int i = gp[x].size(); i != 0; --i)
	{
        int val = gp[x][i-1];
	    allvisited = 0;
	    if(!cats[val]) dfs(val);
	    else if(xcats+1 <= m) dfs(val, xcats+1);
	}
    if(allvisited) ++count;
}
main()
{
	n = readint(), m = readint();
	int leafs = n-1;
	gp=std::vector< std::vector<int> >(n, std::vector<int>(0));
	cats=std::vector<bool>(n);

	for(int i = 0; i < n; ++i)
		cats[i] = readint();
	for(int i = 0; i < n-1; ++i)
	{
		int x = readint(), y = readint();
		x < y ? gp[x-1].push_back(y-1) : gp[y-1].push_back(x-1);
	}
	dfs(0, cats[0]);
	printint(count);
}


#include <iostream>
#include <algorithm>
#include <cmath>
//#include <string>
//#include <queue>
//#include <stack>
//#include <map>
#include <vector>
#include <list>
//#include <memory>
#include <stdio.h>

#define ENDL putchar('\n')
#define SPACE putchar(' ')

bool io_wasDot = false;
int io_lastintsize = 0;
long long readint()
{
	char x = getchar(); bool neg = false; long long result = 0; io_lastintsize = 0;
	while(!isdigit(x) and x != '-') x = getchar();
	result = (x == '-') ? (neg = true)*0 : (x - '0' + (++io_lastintsize)*0);
	while(isdigit((x = getchar())))result = result*10 + x - '0', ++io_lastintsize; io_wasDot = (x == '.');
	return neg ? -result : result;
}
double readfloat()
{
	double res = readint();
	if(io_wasDot) {double tmp = readint(); res += tmp/std::pow(10, io_lastintsize);}
	return res;
}
void readstr(char *str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { *str = c; ++str; c=getchar(); } }
void readstr(std::vector<char> &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }
void readstr(std::string &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }

void printint(long long x){ if(x < 0) { putchar('-'); printint(-x); return; } if(x > 9) printint(x/10);  putchar('0'+x%10); }

void printstr(char *str){ for(char *cur = str; *cur; ++cur) putchar(*cur); }
void printstr(std::string &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }
void printstr(std::vector<char> &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }

int n, m;
std::vector< std::list<int> > gp(0);
std::vector<bool> cats(0);
int count = 0;
void dfs(int x, int from, int xcats=0)
{
	bool allvisited = 1;
	for(auto it = gp[x].begin(); it != gp[x].end(); ++it)
	{
        int val = *it;
	    if(val == from) continue;
	    allvisited = 0;
	    if(!cats[val]) dfs(val, x);
	    else if(xcats+1 <= m) dfs(val, x, xcats+1);
	}
    if(allvisited) ++count;
}
main()
{
	n = readint(), m = readint();
	gp=std::vector< std::list<int> >(n*2);
	cats=std::vector<bool>(n);

	for(int i = 0; i < n; ++i)
		cats[i] = readint();
	for(int i = 0; i < n-1; ++i)
	{
		int x = readint(), y = readint();
		gp[x-1].push_back(y-1); gp[y-1].push_back(x-1);
	}
	dfs(0, -1, cats[0]);
	printint(count);
}


#include <iostream>
#include <algorithm>
#include <cmath>
//#include <string>
//#include <queue>
//#include <stack>
//#include <map>
#include <vector>
//#include <memory>
#include <stdio.h>

#define ENDL putchar('\n')
#define SPACE putchar(' ')

bool io_wasDot = false;
int io_lastintsize = 0;
long long readint()
{
	char x = getchar(); bool neg = false; long long result = 0; io_lastintsize = 0;
	while(!isdigit(x) and x != '-') x = getchar();
	result = (x == '-') ? (neg = true)*0 : (x - '0' + (++io_lastintsize)*0);
	while(isdigit((x = getchar())))result = result*10 + x - '0', ++io_lastintsize; io_wasDot = (x == '.');
	return neg ? -result : result;
}
double readfloat()
{
	double res = readint();
	if(io_wasDot) {double tmp = readint(); res += tmp/std::pow(10, io_lastintsize);}
	return res;
}
void readstr(char *str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { *str = c; ++str; c=getchar(); } }
void readstr(std::vector<char> &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }
void readstr(std::string &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }

void printint(long long x){ if(x < 0) { putchar('-'); printint(-x); return; } if(x > 9) printint(x/10);  putchar('0'+x%10); }

void printstr(char *str){ for(char *cur = str; *cur; ++cur) putchar(*cur); }
void printstr(std::string &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }
void printstr(std::vector<char> &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }

int n, m;
std::vector< std::vector<int> > gp(0);
std::vector<int> cats(0);
int count = 0;
void dfs(int x, int xcats=0)
{
	if(gp[x].size() != 0)
	{
		for(int i = gp[x].size(); i != 0; --i)
		{
		    int val = gp[x][i-1];
		    if(!cats[val]) dfs(val);
		    else if(xcats+1 <= m) dfs(val, xcats+1);
		}
	}
	else if(x != 0)
		++count;

}
main()
{
	n = readint(), m = readint();
	{
		std::vector< std::vector<int> > _gp(n, std::vector<int>(0));
		std::vector<int> _cats(n);
		gp.swap(_gp); cats.swap(_cats);
	}

	for(int i = 0; i < n; ++i)
		cats[i] = readint();
	for(int i = 0; i < n-1; ++i)
	{
		int x = readint(), y = readint();
		gp[x-1].push_back(y-1);
	}
	dfs(0,cats[0]);
	printint(count);
}


#include <iostream>
#include <algorithm>
#include <cmath>
//#include <string>
//#include <queue>
//#include <stack>
//#include <map>
#include <vector>
//#include <memory>
#include <stdio.h>

#define ENDL putchar('\n')
#define SPACE putchar(' ')

bool io_wasDot = false;
int io_lastintsize = 0;
long long readint()
{
	char x = getchar(); bool neg = false; long long result = 0; io_lastintsize = 0;
	while(!isdigit(x) and x != '-') x = getchar();
	result = (x == '-') ? (neg = true)*0 : (x - '0' + (++io_lastintsize)*0);
	while(isdigit((x = getchar())))result = result*10 + x - '0', ++io_lastintsize; io_wasDot = (x == '.');
	return neg ? -result : result;
}
double readfloat()
{
	double res = readint();
	if(io_wasDot) {double tmp = readint(); res += tmp/std::pow(10, io_lastintsize);}
	return res;
}
void readstr(char *str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { *str = c; ++str; c=getchar(); } }
void readstr(std::vector<char> &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }
void readstr(std::string &str, char delim = ' '){ char c = getchar(); while(c != delim and c != EOF) { str.push_back(c); c=getchar(); } }

void printint(long long x){ if(x < 0) { putchar('-'); printint(-x); return; } if(x > 9) printint(x/10);  putchar('0'+x%10); }

void printstr(char *str){ for(char *cur = str; *cur; ++cur) putchar(*cur); }
void printstr(std::string &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }
void printstr(std::vector<char> &str){ for(int i = 0, sz = str.size(); i < sz; ++i) putchar(str[i]); }

int n, m;
std::vector< std::vector<int> > gp(0);
std::vector<int> cats(0);
int count = 0;
void dfs(int x, bool xcats=0)
{
	if(gp[x].size() != 0)
	{
		for(std::vector<int>::iterator a = gp[x].begin(), end = gp[x].end(); a != end; ++a)
			if(xcats + cats[*a] <= m) cats[*a] ? dfs(*a,  xcats + cats[*a]) : dfs(*a);
	}
	else
	{
		++count;
		return;
	}
}
main()
{
	n = readint(), m = readint();
	{
		std::vector< std::vector<int> > _gp(n, std::vector<int>(0));
		std::vector<int> _cats(n);
		gp.swap(_gp); cats.swap(_cats);
	}

	for(int i = 0; i < n; ++i)
		cats[i] = readint();
	for(int i = 0; i < n-1; ++i)
	{
		int x = readint(), y = readint();
		gp[x-1].push_back(y-1);
	}
	dfs(0,cats[0]);
	printint(count);
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn],sum[maxn];
vector<int> vec[maxn];
int n,m;
void dfs(int u,int su)
{
    if(!vec[u].size()&&su+a[u]<=m&&u!=1)
        sum[u]=1;
    for(int i=0;i<vec[u].size();i++)
    {
        if(a[u])
            dfs(vec[u][i],su+1);
        else
            dfs(vec[u][i],0);
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            vec[i].clear();
        }
        memset(sum,0,sizeof(sum));
        int flag=0;
        for(int i=1; i<n; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            if(u==v) continue;
            vec[u].push_back(v);
        }
        dfs(1,0);
        int ans=0;
        for(int i=1;i<=n;i++)
            ans+=sum[i];
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector< int > adj[100005];
vector< int > visited(100005,false);
int cats[100005];
int ans=0,m;
void dfs(int u,int pnct)
    {
    if(visited[u]==true)
        {
        return ;
    }
    visited[u]=true;
    int f=0,cnt;
    for(int i=0;i<adj[u].size();i++)
        {
        if(adj[u].size()<=1&&u!=1)
            {
            break;
        }
        cnt=pnct;
        int v=adj[u][i];
        if(cnt!=0)
            {
            if(cats[v]!=1)
                {
                cnt=0;
            }
            else
                {
                cnt++;
            }
        }
        else if(cnt==0)
            {
            if(cats[v]==1)
                {
                cnt++;
            }
        }
        if(cnt>m)
            {
            f=1;
            continue;
        }
        dfs(v,cnt);
    }
    if(pnct>m)
        {
        f=1;
    }
    if(f==0&&adj[u].size()==1&&u!=1)
        {
        ans++;
    }

}
int main()
    {

    std::ios::sync_with_stdio(false);
    cin.tie(0);
    int i,t,e,n;

    cin>>n>>m;
    for(i=1;i<=n;i++)
        {
        cin>>cats[i];
    }
    for(i=0;i<n-1;i++)
        {
        int u,v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(1,cats[1]);
    cout<<ans<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn],sum[maxn];
vector<int> vec[maxn];
int n,m;
void dfs(int u,int su)
{
    if(!vec[u].size()&&su+a[u]<=m)
        sum[u]=1;
    for(int i=0;i<vec[u].size();i++)
    {
        if(a[u])
            dfs(vec[u][i],su+1);
        else
            dfs(vec[u][i],0);
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            vec[i].clear();
        }
        memset(sum,0,sizeof(sum));
        int flag=0;
        for(int i=1; i<n; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            if(u==v) continue;
            if(v==1) flag=1;
            vec[u].push_back(v);
        }
        if(flag)
        {
            puts("0");
            continue;
        }
        dfs(1,0);
        int ans=0;
        for(int i=1;i<=n;i++)
            ans+=sum[i];
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn],sum[maxn],ff[maxn];
vector<int> vec[maxn];
int n,m;
void inint()
{
    for(int i=1; i<=n; i++)
    {
        vec[i].clear();
    }
    memset(ff,0,sizeof(ff));
    memset(sum,0,sizeof(sum));
}
void dfs(int u,int ans)
{
    //printf("++++%d %d\n",u,ans);
    int flag=1,aa=0;
    if(a[u])
        aa=1;
    ff[u]=1;
    for(int i=0;i<vec[u].size();i++)
    {
        if(ff[vec[u][i]]) continue;
        flag=0;
        if(aa)
            dfs(vec[u][i],ans+1);
        else
            dfs(vec[u][i],0);
    }
    if(flag&&ans+aa<=m)
        sum[u]=1;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        inint();
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1; i<n; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            vec[u].push_back(v);
            vec[v].push_back(u);
        }
            dfs(1,0);
        int ans=0;
        for(int i=1;i<=n;i++)
            ans+=sum[i];
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define nl printf("\n")
#define f(i,l1,l2) for(i=l1;i<l2;i++)
#define vi vector<int>
#define vit vi::iterator
#define all(c) c.begin(), c.end()
#define pb push_back
using namespace std;
vector< int > adj[100005];
vector< int > visited(100005,false);
int cats[100005];
int ans=0,m;
void dfs(int u,int pnct)
    {
    if(visited[u]==true)
        {
        return ;
    }
    visited[u]=true;
    int f=0,cnt;
    for(int i=0;i<adj[u].size();i++)
        {
        if(adj[u].size()<=1&&u!=1)
            {
            break;
        }
        cnt=pnct;
        int v=adj[u][i];
        if(cnt!=0)
            {
            if(cats[v]!=1)
                {
                cnt=0;
            }
            else
                {
                cnt++;
            }
        }
        else if(cnt==0)
            {
            if(cats[v]==1)
                {
                cnt++;
            }
        }
        if(cnt>m)
            {
            f=1;
            continue;
        }
        dfs(v,cnt);
    }
    if(pnct>m)
        {
        f=1;
    }
    if(f==0&&adj[u].size()==1&&u!=1)
        {
        ans++;
    }

}
int main()
    {

    std::ios::sync_with_stdio(false);
    cin.tie(0);
    int i,t,e,n;

    cin>>n>>m;
    for(i=1;i<=n;i++)
        {
        cin>>cats[i];
    }
    for(i=0;i<n-1;i++)
        {
        int u,v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(1,cats[1]);
    cout<<ans<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn],sum[maxn],ff[maxn];
vector<int> vec[maxn];
int n,m;
void inint()
{
    for(int i=1; i<=n; i++)
    {
        vec[i].clear();
        sum[i]=0;
        ff[i]=0;
    }
}
void dfs(int u,int ans)
{
    //printf("++++%d %d\n",u,ans);
    int flag=1,aa=0;
    if(a[u])
        aa=1;
    ff[u]=1;
    for(int i=0;i<vec[u].size();i++)
    {
        if(ff[vec[u][i]]) continue;
        flag=0;
        if(aa)
            dfs(vec[u][i],ans+1);
        else
            dfs(vec[u][i],0);
    }
    if(flag&&ans+aa<=m)
        sum[u]=1;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        inint();
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1; i<n; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            vec[u].push_back(v);
            vec[v].push_back(u);
        }
            dfs(1,0);
        int ans=0;
        for(int i=1;i<=n;i++)
            ans+=sum[i];
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn],sum[maxn];
vector<int> vec[maxn];
int n,m;
void inint()
{
    for(int i=1; i<=n; i++)
    {
        vec[i].clear();
        sum[i]=0;
    }
}
void dfs(int u,int ans,int pre)
{
    //printf("++++%d %d\n",u,ans);
    int flag=1,aa=0;
    if(a[u])
        aa=1;
    for(int i=0;i<vec[u].size();i++)
    {
        if(vec[u][i]==pre) continue;
        flag=0;
        if(aa)
            dfs(vec[u][i],ans+1,u);
        else
            dfs(vec[u][i],0,u);
    }
    if(flag&&ans+aa<=m)
        sum[u]=1;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        inint();
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1; i<n; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            if(u==v) continue;
            vec[u].push_back(v);
            vec[v].push_back(u);
        }
            dfs(1,0,0);
        int ans=0;
        for(int i=1;i<=n;i++)
            ans+=sum[i];
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn],sum[maxn];
vector<int> vec[maxn];
int n,m;
void dfs(int u,int su)
{
    if(!vec[u].size()&&su+a[u]<=m)
        sum[u]=1;
    for(int i=0;i<vec[u].size();i++)
    {
        if(a[u])
            dfs(vec[u][i],su+1);
        else
            dfs(vec[u][i],su+1);
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            vec[i].clear();
        }
        memset(sum,0,sizeof(sum));
        int flag=0;
        for(int i=1; i<n; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            if(u==v) continue;
            if(v==1) flag=1;
            vec[u].push_back(v);
        }
        if(flag)
        {
            puts("0");
            continue;
        }
        dfs(1,0);
        int ans=0;
        for(int i=1;i<=n;i++)
            ans+=sum[i];
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn],sum[maxn],ff[maxn];
vector<int> vec[maxn];
int n,m;
void inint()
{
    for(int i=1; i<=n; i++)
    {
        vec[i].clear();
    }
    memset(ff,0,sizeof(ff));
    memset(sum,0,sizeof(sum));
}
void dfs(int u,int ans)
{
    //printf("++++%d %d\n",u,ans);
    int flag=1,aa=0;
    if(a[u])
        aa=1;
    ff[u]=1;
    for(int i=0;i<vec[u].size();i++)
    {
        flag=0;
        if(ff[vec[u][i]]) continue;
        if(aa)
            dfs(vec[u][i],ans+1);
        else
            dfs(vec[u][i],0);
    }
    if(flag&&ans+aa<=m)
        sum[u]=1;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        inint();
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1; i<n; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            vec[u].push_back(v);
            vec[v].push_back(u);
        }
            dfs(1,0);
        int ans=0;
        for(int i=1;i<=n;i++)
            ans+=sum[i];
        printf("%d\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn],sum[maxn];
vector<int> vec[maxn];
int n,m;
void inint()
{
    for(int i=1; i<=n; i++)
    {
        vec[i].clear();
        sum[i]=0;
    }
}
void dfs(int u,int ans)
{
    //printf("++++%d %d\n",u,ans);
    int flag=1,aa=0;
    if(a[u])
        aa=1;
    for(int i=0;i<vec[u].size();i++)
    {
        flag=0;
        if(aa)
            dfs(vec[u][i],ans+1);
        else
            dfs(vec[u][i],0);
    }
    if(flag&&ans+aa<=m)
        sum[u]=1;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        inint();
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1; i<n; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            vec[u].push_back(v);
        }
            dfs(1,0);
        int ans=0;
        for(int i=1;i<=n;i++)
            ans+=sum[i];
        printf("%d\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
namespace FastIO {
	const int FastIO_Max = 100000; static int FastIO_wpos, FastIO_xlen, FastIO_xpos; static char FastIO_wbuf[FastIO_Max], FastIO_xbuf[FastIO_Max]; void xint() {}void wint() {}
	void xchar(char& var) { var = (FastIO_xpos == FastIO_xlen && (FastIO_xlen = (FastIO_xpos = 0) + fread(FastIO_xbuf, 1, FastIO_Max, stdin), FastIO_xpos == FastIO_xlen) ? EOF : FastIO_xbuf[FastIO_xpos++]); }
	template <typename T1, typename... T2>void xint(T1& var, T2&... oth) { char ch; for (xchar(ch); (ch > '9' || ch < '0') && ch != '-'; xchar(ch)); int sym = (ch == '-' ? xchar(ch), -1 : 1); for (var = 0; ch >= '0' && ch <= '9'; xchar(ch))var = var * 10 + ch - '0'; var *= sym; xint(oth...); }
	void wchar(char ch) { FastIO_wpos == FastIO_Max ? fwrite(FastIO_wbuf, 1, FastIO_Max, stdout), FastIO_wpos = 0, FastIO_wbuf[FastIO_wpos++] = ch : FastIO_wbuf[FastIO_wpos++] = ch; }void wend() { fwrite(FastIO_wbuf, 1, FastIO_wpos, stdout); }
	template <typename T1, typename... T2>void wint(T1 val, T2... oth) { if (sizeof(val) == 1)wchar(val); else { val = (val < 0 ? wchar('-'), -val : val); char out_save[50] = {}; int n; for (n = 0; val || !n; val /= 10)out_save[n++] = '0' + val % 10; while (n--)wchar(out_save[n]); }wint(oth...); }
}
using namespace FastIO;
using namespace std;

#define ll long long
#define inf 0x3f3f3f3f
#define maxn (int)(1e5 + 5)

int n, m, num, ans;
int a[maxn];
vector<int> r[maxn];

void dfs(int x, int p)
{
	if (num > m)
		return;
	if (r[x].size() <= 1 && x != 1)
	{
		ans++;
		return;
	}
	for (int i = 0; i < r[x].size(); i++)
	{
		if (r[x][i] == p)
			continue;
		int tmp = num;
		if (a[r[x][i]])
			num++;
		else
			num = 0;
		dfs(r[x][i], x);
		num = tmp;
	}
}

int main()
{
	xint(n, m);
	for (int i = 1; i <= n; i++)
		xint(a[i]);
	for (int i = 1, u, v; i < n; i++)
	{
		xint(u, v);
		r[u].push_back(v);
		r[v].push_back(u);
	}

	num += a[1];
	dfs(1, 1);
	wint(ans);
	wend();
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll MX=100005;ll m,tot,in[MX],vis[MX],fir[MX],val[MX];struct STR{ll next,to;}edge[MX<<1];
void add(ll u,ll v,ll cnt){edge[cnt].to=v;edge[cnt].next=fir[u];fir[u]=cnt;}
void dfs(ll x,ll now_cnt,ll _max){
	if(!(in[x]-1)){if(_max<=m)tot++;return;}
	if(_max>m)return;vis[x]=1;
	for(int i=fir[x];i;i=edge[i].next)
		if(!vis[edge[i].to]){
			ll q=val[edge[i].to]?now_cnt+1:0;dfs(edge[i].to,q,max(_max,q));
		}
}
int main(){
	ll n;scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;i++)scanf("%lld",&val[i]);
	for(int i=1;i<=n-1;i++){ll u,v;scanf("%lld%lld",&u,&v);add(u,v,i);add(v,u,i+n-1);in[u]++;in[v]++;}
	dfs(1,val[1],val[1]);printf("%lld",tot);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;

int p[100005];
struct node{
	int next,to;
}a[200005];
int head[100005];
int c[100005];
int top,n,m;

void add(int u,int v){
	a[++top].next=head[u];
	a[top].to=v;
	head[u]=top;
}

int ans;

void dfs(int u,int fa,int now){
	for (int i=head[u];i;i=a[i].next){
		int v=a[i].to;
		if (v==fa)
			continue;
		if (p[v]+now>m){
			continue;
		}
		if (c[v]==1&&v!=0)
			if (now+p[v]<=m){
				ans++;
				continue;
			}
		if (now>0&&(!p[v]))
			dfs(v,u,0);
		else
			dfs(v,u,now+p[v]);
	}
	return;
}

int main(){
	//freopen (".in","r",stdin);
	//freopen (".out","w",stdout);
	scanf ("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
		scanf ("%d",p+i);
	int u,v;
	for (int i=1;i<n;i++){
		scanf ("%d%d",&u,&v);
		add(u,v);
		add(v,u);
		c[u]++;
		c[v]++;
	}
	dfs(1,0,p[1]);
	printf ("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MX=100005;int m,tot,in[MX],vis[MX],fir[MX],val[MX];struct STR{int next,to;}edge[MX<<1];
void add(int u,int v,int cnt){edge[cnt].to=v;edge[cnt].next=fir[u];fir[u]=cnt;}
void dfs(int x,int now_cnt,int _max){
	if(!(in[x]-1)&&x-1){if(_max<=m)tot++;return;}
	vis[x]=1;
	for(int i=fir[x];i;i=edge[i].next)
		if(!vis[edge[i].to]){
			int q=val[edge[i].to]?now_cnt+1:0;dfs(edge[i].to,q,max(_max,q));
		}
}
int main(){
	int n;scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)scanf("%d",&val[i]);
	for(int i=1;i<=n-1;i++){int u,v;scanf("%d%d",&u,&v);add(u,v,i);add(v,u,i+n-1);in[u]++;in[v]++;}
	dfs(1,val[1],val[1]);printf("%d",tot);
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 500000;
struct Node
{
	int v, next;
}p[maxn*2];
int head[maxn], cnt, a[maxn],n,m,ans;
void add(int u, int v)
{
	p[++cnt].v = v;
	p[cnt].next = head[u];
	head[u] = cnt;
}
void DFS(int u,int f,int w)
{
	bool flag =0;
	for (int i = head[u]; i; i = p[i].next)
	{
		int v = p[i].v;
		if (v == f)
		{
			continue;
		}
		flag = 1;
		if (a[v] + w > m)
		{
			continue;
		}
		if (a[v])
		{
			DFS(v, u, a[v] + w);
		}
		else
		{
			DFS(v, u, 0);
		}
	}
	if (!flag)
	{
		ans++;
	}
}
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	for (int i = 1; i <= n-1; i++)
	{
		int a, b;
		cin >> a>>b;
		add(a, b);
		add(b, a);
	}
	DFS(1, 0, a[1]);
	cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;
const int N = 100005;

int arr[N] , n , m , ans = 0;
vector<int> a[N];

void dfs(int now,int val)
{
	if(val > m && a[now].size() != 0)return;
	if(a[now].size() == 0 && val <= m)
	{
		ans++;
		return;
	}
	for(int i = 0;i < a[now].size();i++)
	{
		if(arr[a[now][i]] != 0)
		dfs(a[now][i],val + arr[a[now][i]]);
		else dfs(a[now][i],0);
	}
}

void input()
{
	cin >> n >> m;
	for(int i = 1;i <= n;i ++)
		scanf("%d",&arr[i]);
	for(int i = 0;i < n - 1;i ++)
	{
		int x , y;
		scanf("%d %d",&x , &y);
		a[x].push_back(y);
	}
}
int main()
{
	input();
	dfs(1,arr[1]);
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
const int Maxn=100000+10,inf=0x3f3f3f3f;
vector <int> e[Maxn];
int a[Maxn],s[Maxn],f[Maxn];
int n,m,ans,tot;
inline int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
void dfs(int x,int fa,int ma)
{
	s[++tot]=a[x];
	f[tot]=s[tot];
	if(f[tot]>0)f[tot]+=f[tot-1];

	bool flag=0;
	for(int i=0;i<e[x].size();++i)
	{
		int y=e[x][i];
		if(y==fa)continue;
		flag=1;
		dfs(y,x,max(ma,f[tot]));
	}
	if(!flag && max(ma,f[tot])<=m)++ans;

	f[tot]=0,s[tot--]=0;
}
int main()
{
	//freopen("in.txt","r",stdin);
	n=read(),m=read();
	for(int i=1;i<=n;++i)
	a[i]=read();
	for(int i=1;i<n;++i)
	{
		int x=read(),y=read();
		e[x].push_back(y);
		e[y].push_back(x);
	}
	dfs(1,0,0);
	printf("%d\n",ans);
	return 0;
}


#include <stdio.h>
#include <algorithm>
#define MX 100005
#define Fo(i,a,b) for(int i=(a);i<(b);i++)
#define Fr(i,a,b) for(int i=(a);i<=(b);i++)
#define Frn(i,a,b) for(int i=(a);i>=(b);i--)
int N,M,F[MX],X[MX],A[MX],B[MX],C[MX],L[MX],Ans;
void sou(int i)
{
	if(B[i])return;
	B[i]=1,sou(F[i]);
	C[i]=X[i]?C[F[i]]+1:0;
	A[i]=std::max(A[F[i]],C[i]);
}
int main()
{
	scanf("%d%d",&N,&M);
	Fr(i,1,N)scanf("%d",&X[i]); int a,b;
	Fo(i,1,N)scanf("%d%d",&a,&b),F[b]=a,L[a]=1;
	B[1]=1,C[1]=X[1],A[1]=X[1];
	Fr(i,1,N)sou(i);
	Fr(i,1,N)if(!L[i]&&A[i]<=M)Ans++;
	printf("%d",Ans);
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int point[1000000];
int head[1000000];
int ver[1000000];
int nxt[1000000];
int cnt;
int out[1000000];
bool leaf[1000000];
int n;
int m;
int ans;
void add(int x,int y)
{
    nxt[++cnt] = head[x];
    head[x] = cnt;
    ver[cnt] = y;
}
void dfs(int now,int father,int already)
{
    bool flag = false;
    for (int i = head[now]; i;i=nxt[i])
    {
        int v0 = ver[i];
        if(v0==father)
            continue;
        flag = true;
        if(point[v0]==0)
            dfs(v0, now, 0);
        else
        {
            if(already+1<=m)
                dfs(v0, now, already + 1);
        }
    }
    if(!flag)
        ans++;
}
int main()
{
    scanf("%d%d", &n,&m);
    for (int i = 1; i <= n;i++)
        scanf("%d", &point[i]);
    for (int i = 1; i < n;i++)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        add(a, b);
        add(b, a);
    }
    dfs(1,0,point[1]);
    printf("%d\n", ans);
}


#include<cstdio>
#include<vector>
#define N 100005
using namespace std;
int n,m,ans;
bool b[N];
vector<int>a[N];
void dfs(int x,int f,int d){
	if(d>m)return;
	for(int i=0;i<a[x].size();i++){
		int y=a[x][i],t=d;
		if(y==f)continue;
		if(b[y])d++;
		else d=0;
		dfs(y,x,d);
		if(b[y])d--;
		else d=t;
	}
	if((int)a[x].size()==1&&x!=1)
		if(d<=m)ans++;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		if(x)b[i]=1;
	}
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		a[x].push_back(y);
		a[y].push_back(x);
	}
	if(b[1])dfs(1,0,1);
	else dfs(1,0,0);
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#define debug puts("wn")
#define R register
#define next mLg
using namespace std;
typedef int ll;
typedef long double ld;
typedef unsigned long long ull;
typedef double dl;
template<typename T>void read(T &x);
template<typename T>void write(T x);
template<typename T>void writesp(T x);
template<typename T>void writeln(T x);
const ll N=2e5+5;

ll head[N], to[N<<1], tot, next[N<<1];
inline void add(ll x, ll y){
	to[++tot]=y; next[tot]=head[x]; head[x]=tot;
}
inline void Link(ll x, ll y){
	add(x, y); add(y, x);
}

ll n, m;
bool flag[N];
ll f[N], g[N], ans;
bool n_leaf[N];

inline void dfs(ll x, ll fa){
	if(flag[x]) g[x]=g[fa]+1;
	else g[x]=0;
	f[x]=max(f[fa], g[x]);
	for(R ll i=head[x], ver; i; i=next[i]){
		ver=to[i];
		if(ver==fa) continue;
		if(!n_leaf[x]) n_leaf[x]=true;
		dfs(ver, x);
	}
}

int main(){
	read(n); read(m);
	for(R ll i=1; i<=n; i++) read(flag[i]);
	for(R ll i=1, x, y; i<n; i++){
		read(x); read(y);
		Link(x, y);
	}
	dfs(1, 0);
	for(R ll i=1; i<=n; i++) ans+=(f[i]<=m && !n_leaf[i])?1:0;
	writeln(ans);
}
template<typename T>void read(T &x){
	x=0; T t=1;
	char wn=getchar();
	while(wn<'0' || wn>'9'){
		if(wn=='-') t=-1;
		wn=getchar();
	}
	while(wn>='0' && wn<='9'){
		x=x*10+wn-'0';
		wn=getchar();
	}
	x*=t;
}
template<typename T>void write(T x){
	if(x<0){putchar('-'); x=-x;}
	if(x<=9){putchar(x+'0'); return;}
	write(x/10); putchar(x%10+'0');
}
template<typename T>void writesp(T x){
	write(x); putchar(' ');
}
template<typename T>void writeln(T x){
	write(x); putchar('\n');
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#define debug puts("wn")
#define R register
#define next mLg
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef double dl;
template<typename T>void read(T &x);
template<typename T>void write(T x);
template<typename T>void writesp(T x);
template<typename T>void writeln(T x);
const ll N=2e5+5;

ll head[N], to[N<<1], tot, next[N<<1];
inline void add(ll x, ll y){
	to[++tot]=y; next[tot]=head[x]; head[x]=tot;
}
inline void Link(ll x, ll y){
	add(x, y); add(y, x);
}

ll n, m;
bool flag[N];
ll f[N], g[N], ans;
bool n_leaf[N];

inline void dfs(ll x, ll fa){
	if(flag[x]) g[x]=g[fa]+1;
	else g[x]=0;
	f[x]=max(f[fa], g[x]);
	for(R ll i=head[x], ver; i; i=next[i]){
		ver=to[i];
		if(ver==fa) continue;
		if(!n_leaf[x]) n_leaf[x]=true;
		dfs(ver, x);
	}
}

int main(){
	read(n); read(m);
	for(R ll i=1; i<=n; i++) read(flag[i]);
	for(R ll i=1, x, y; i<n; i++){
		read(x); read(y);
		Link(x, y);
	}
	dfs(1, 0);
	for(R ll i=1; i<=n; i++) ans+=(f[i]<=m && !n_leaf[i])?1:0;
	writeln(ans);
}
template<typename T>void read(T &x){
	x=0; T t=1;
	char wn=getchar();
	while(wn<'0' || wn>'9'){
		if(wn=='-') t=-1;
		wn=getchar();
	}
	while(wn>='0' && wn<='9'){
		x=x*10+wn-'0';
		wn=getchar();
	}
	x*=t;
}
template<typename T>void write(T x){
	if(x<0){putchar('-'); x=-x;}
	if(x<=9){putchar(x+'0'); return;}
	write(x/10); putchar(x%10+'0');
}
template<typename T>void writesp(T x){
	write(x); putchar(' ');
}
template<typename T>void writeln(T x){
	write(x); putchar('\n');
}


#include<bits/stdc++.h>
#define re register int
#define rep(q,a,b) for(re q(a);q<=b;++q)
#define dep(q,a,b) for(re q(a);q>=b;--q)
#define mem(a,b) memset(a,b,sizeof a )
#define travel(at) for(re q=head[at];q;q=ne[q])
#define link(a,b) link_edge(a,b),link_edge(b,a)
#define link_edge(a,b) to[++cnt1]=b,ne[cnt1]=head[a],head[a]=cnt1
using namespace std;
template <typename T>
inline void in(T &r) {
	r=0;
	char c;
	int flag(0);
	while(c=getchar(),!isdigit(c))(c=='-')&&(flag=1);
	do {
		r=(r<<1)+(r<<3)+(c^48);
	} while(c=getchar(),isdigit(c));
	(flag)&&(r=-r);
}
int ou[20];
template <typename T>
inline void out(T x) {
	if(x<0)putchar('-'),x=-x;
	else if(!x) {
		putchar('0');
		return;
	}
	int cnt(0);
	while(x)ou[++cnt]=x%10,x/=10;
	for(int q=cnt; q; --q)putchar(ou[q]^48);
}
const int mn=100005;
int head[mn],ne[mn<<1],to[mn<<1],n,m,cnt1=0,ans=0;
bool val[mn];
void dfs(int f,int at,int v){
	if(v>m)return;
    bool ok=1;
	travel(at)if(to[q]!=f)dfs(at,to[q],val[to[q]]==1?v+1:0),ok=0;
	ans+=ok;
}
int main() {
	re a,b;
	in(n),in(m);
	rep(q,1,n)in(val[q]);
	rep(q,1,n-1)in(a),in(b),link(a,b);
	dfs(0,1,val[1]);
	printf("%d\n",ans);
	return 0;
}