import java.util.*;
public final class KefaPark
{ static boolean[] marked;
static ArrayList<Integer>[] arr;
static int[] a;
static int c=0,m;
	public static void main(String[] args) {
		Scanner br=new Scanner(System.in);
		int n=br.nextInt();
		m=br.nextInt();
		int ans=0;
		marked=new boolean[n];
		ArrayList<Integer> leaf=new ArrayList<Integer>();
		arr=new  ArrayList[n];
		a=new int[n];
		for(int i=0;i<n;i++)
		{
			a[i]=br.nextInt();
			arr[i]=new ArrayList<Integer>();
		}
		for(int i=0;i<n-1;i++)
		{
			int x=br.nextInt()-1;
			int y=br.nextInt()-1;
			arr[x].add(y);
			arr[y].add(x);
		}
		for(int i=1;i<n;i++)
		{
			if(arr[i].size()==1)
				leaf.add(i);
		}
		dfs(0,0);

		for(Integer i:leaf)
			if(marked[i])
				ans++;
			System.out.println(ans);

	}
	public static void dfs(int v,int c)
	{

		if(a[v]==1)
			c++;
		else
			c=0;
		if(c>m)
			return ;
		marked[v]=true;
		for(int i:arr[v])
		{
			if(!marked[i])
				dfs(i,c);
		}
		return;
	}

}


import java.io.*;
import java.util.*;

public class P580C {
	static boolean[] cats;
	public static void main(String[] args) {
		NewReader sc = new NewReader();
		PrintWriter pw = new PrintWriter(System.out, true);
		int N = sc.nextInt();
		int M = sc.nextInt();
		cats = new boolean[N+1];
		HashMap<Integer, Tree> idToNode = new HashMap<Integer, Tree>();
		for (int i = 0; i < N; i++) {
			cats[i+1] = sc.nextInt() == 1 ? true : false;
		}
		for (int i = 1; i < N; i++) {
			int from = sc.nextInt();
			int to = sc.nextInt();
			if (!idToNode.containsKey(from)) {
				Tree t = new Tree(from);
				idToNode.put(from, t);
			}
			if (!idToNode.containsKey(to)) {
				Tree t = new Tree(to);
				idToNode.put(to, t);
			}
			idToNode.get(from).nbrs.add(idToNode.get(to));
			idToNode.get(to).nbrs.add(idToNode.get(from));
		}
		pw.println(getRestaurants(idToNode.get(1), 0, M, -1));
	}
	static int getRestaurants(Tree root, int cat, int M, int parent) {
		if (cats[root.id]) {
			cat++;
		} else {
			cat = 0;
		}

		if (cat > M) return 0;
		if (root.nbrs.size() == 1 && root.nbrs.get(0).id==parent) return 1;

		int total = 0;
		for (Tree nbr : root.nbrs) {
			if(nbr.id==parent) continue;
			total += getRestaurants(nbr, cat, M, root.id);
		}
		return total;
	}
}

class Tree {
	int id;
	List<Tree> nbrs;
	Tree(int id) {
		this.id = id;
		nbrs = new ArrayList<Tree>();
	}
}

class NewReader {
	InputStream is;

	public NewReader() {
		is = System.in;
	}

	String nextString() {

		int b = skip();
		StringBuilder sb = new StringBuilder();
		while (!(isSpaceChar(b))) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	byte[] inbuf = new byte[1024];
	int lenbuf = 0, ptrbuf = 0;

	int readByte() {

		if (lenbuf == -1)
			throw new InputMismatchException();
		if (ptrbuf >= lenbuf) {
			ptrbuf = 0;
			try {
				lenbuf = is.read(inbuf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (lenbuf <= 0)
				return -1;
		}
		return inbuf[ptrbuf++];
	}

	int nextInt() {

		int num = 0, b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	char nextChar() {

		return (char) skip();
	}

	boolean isSpaceChar(int c) {

		return !(c >= 33 && c <= 126);
	}

	int skip() {

		int b;
		while ((b = readByte()) != -1 && isSpaceChar(b))
			;
		return b;
	}

	long nextLong() {

		long num = 0;
		int b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
}


import java.io.*;
import java.util.*;

public class P580C {
	static boolean[] cats;
	public static void main(String[] args) {
		NewReader sc = new NewReader();
		PrintWriter pw = new PrintWriter(System.out, true);
		int N = sc.nextInt();
		int M = sc.nextInt();
		cats = new boolean[N+1];
		HashMap<Integer, Tree> idToNode = new HashMap<Integer, Tree>();
		for (int i = 0; i < N; i++) {
			cats[i+1] = sc.nextInt() == 1 ? true : false;
		}
		for (int i = 1; i < N; i++) {
			int from = sc.nextInt();
			int to = sc.nextInt();
			if (!idToNode.containsKey(from)) {
				Tree t = new Tree(from);
				idToNode.put(from, t);
			}
			if (!idToNode.containsKey(to)) {
				Tree t = new Tree(to);
				idToNode.put(to, t);
			}
			idToNode.get(from).nbrs.add(idToNode.get(to));
			idToNode.get(to).nbrs.add(idToNode.get(from));
		}
		pw.println(getRestaurants(idToNode.get(1), 0, M, -1));
	}
	static int getRestaurants(Tree root, int cat, int M, int parent) {
		if (cats[root.id]) {
			cat++;
		} else {
			cat = 0;
		}

		if (cat > M) return 0;
		if (root.nbrs.size() == 1) return 1;

		int total = 0;
		for (Tree nbr : root.nbrs) {
			if(nbr.id==parent) continue;
			total += getRestaurants(nbr, cat, M, root.id);
		}
		return total;
	}
}

class Tree {
	int id;
	List<Tree> nbrs;
	Tree(int id) {
		this.id = id;
		nbrs = new ArrayList<Tree>();
	}
}

class NewReader {
	InputStream is;

	public NewReader() {
		is = System.in;
	}

	String nextString() {

		int b = skip();
		StringBuilder sb = new StringBuilder();
		while (!(isSpaceChar(b))) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	byte[] inbuf = new byte[1024];
	int lenbuf = 0, ptrbuf = 0;

	int readByte() {

		if (lenbuf == -1)
			throw new InputMismatchException();
		if (ptrbuf >= lenbuf) {
			ptrbuf = 0;
			try {
				lenbuf = is.read(inbuf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (lenbuf <= 0)
				return -1;
		}
		return inbuf[ptrbuf++];
	}

	int nextInt() {

		int num = 0, b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	char nextChar() {

		return (char) skip();
	}

	boolean isSpaceChar(int c) {

		return !(c >= 33 && c <= 126);
	}

	int skip() {

		int b;
		while ((b = readByte()) != -1 && isSpaceChar(b))
			;
		return b;
	}

	long nextLong() {

		long num = 0;
		int b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
}


import java.io.*;
import java.util.*;

public class P580C {
	static boolean[] cats;
	public static void main(String[] args) {
		NewReader sc = new NewReader();
		PrintWriter pw = new PrintWriter(System.out, true);
		int N = sc.nextInt();
		int M = sc.nextInt();
		cats = new boolean[N+1];
		HashMap<Integer, Tree> idToNode = new HashMap<Integer, Tree>();
		for (int i = 0; i < N; i++) {
			cats[i+1] = sc.nextInt() == 1 ? true : false;
		}
		for (int i = 1; i < N; i++) {
			int from = sc.nextInt();
			int to = sc.nextInt();
			if (!idToNode.containsKey(from)) {
				Tree t = new Tree(from);
				idToNode.put(from, t);
			}
			if (!idToNode.containsKey(to)) {
				Tree t = new Tree(to);
				idToNode.put(to, t);
			}
			idToNode.get(from).nbrs.add(idToNode.get(to));
		}
		pw.println(getRestaurants(idToNode.get(1), 0, M));
	}
	static int getRestaurants(Tree root, int cat, int M) {
		if (cats[root.id]) {
			cat++;
		} else {
			cat = 0;
		}

		if (cat > M) return 0;
		if (root.nbrs.size() == 0) return 1;

		int total = 0;
		for (Tree nbr : root.nbrs) {
			total += getRestaurants(nbr, cat, M);
		}
		return total;
	}
}

class Tree {
	int id;
	List<Tree> nbrs;
	Tree(int id) {
		this.id = id;
		nbrs = new ArrayList<Tree>();
	}
}

class NewReader {
	InputStream is;

	public NewReader() {
		is = System.in;
	}

	String nextString() {

		int b = skip();
		StringBuilder sb = new StringBuilder();
		while (!(isSpaceChar(b))) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	byte[] inbuf = new byte[1024];
	int lenbuf = 0, ptrbuf = 0;

	int readByte() {

		if (lenbuf == -1)
			throw new InputMismatchException();
		if (ptrbuf >= lenbuf) {
			ptrbuf = 0;
			try {
				lenbuf = is.read(inbuf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (lenbuf <= 0)
				return -1;
		}
		return inbuf[ptrbuf++];
	}

	int nextInt() {

		int num = 0, b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	char nextChar() {

		return (char) skip();
	}

	boolean isSpaceChar(int c) {

		return !(c >= 33 && c <= 126);
	}

	int skip() {

		int b;
		while ((b = readByte()) != -1 && isSpaceChar(b))
			;
		return b;
	}

	long nextLong() {

		long num = 0;
		int b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
}


import java.awt.Point;
import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.ObjectInputStream.GetField;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.security.KeyStore.Entry;
import java.text.DecimalFormat;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.Formatter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentMap;

public class Solution {
    static FastReader in = new FastReader(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter out = new PrintWriter(System.out);
    static boolean file = false;
    static final int maxn = (int)1e5+111;
    static int inf = (int)1e9;
    static int n,m,d;
    static int cat[] = new int[maxn];
    static int deg[] = new int[maxn];
    static int ans[] = new int[maxn];
    static ArrayList<Integer> a[] = new ArrayList[maxn];

    private static void solve() throws Exception {
        n = in.nextInt();
        m = in.nextInt();

        for (int i=1; i<=n; i++) {
            a[i] = new ArrayList<Integer>();
            ans[i] = -1;
            cat[i] = in.nextInt();
        }

        for (int i=1; i<n; i++) {
            int from = in.nextInt();
            int to = in.nextInt();
            deg[from]++;
            deg[to]++;
            a[from].add(to);
            a[to].add(from);
        }

        rec(1,cat[1], 0);
        int cnt=0;
        for (int i=1; i<=n; i++) {
            if (deg[i]==1) {
                if (ans[i]!=-1) {
                    if (ans[i]<=m) {
                        cnt++;
                    }
                }
            }
        }

        out.println(cnt);
    }

    public static void rec(int v, int sum, int p) {
        if (sum>m) return;
        ans[v] = sum;
        for (int i=0; i<a[v].size(); i++) {
            int to = a[v].get(i);
            if (to==p) continue;
            if (cat[to]==0) {
                rec(to,0,v);
            } else {
                rec(to,sum+1,v);
            }
        }
    }

    public static void main (String [] args) throws Exception {

        if (file) {
            in = new FastReader(new BufferedReader(new FileReader("input.txt")));
            out = new PrintWriter ("output.txt");
        }

        solve();
        out.close();
    }
}

class Pair implements Comparable<Pair> {
    int x,y;

    public Pair (int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public int compareTo(Pair p) {
        if (x>p.x) return 1;
        else if (x==p.x) return 0;
        else return -1;
    }
}

class FastReader {
    BufferedReader bf;
    StringTokenizer tk = null;

    public FastReader(BufferedReader bf) {
        this.bf = bf;
    }

    public String nextToken () throws Exception {
        if (tk==null || !tk.hasMoreTokens()) {
            tk = new StringTokenizer(bf.readLine());
        }
        return tk.nextToken();
    }

    public int nextInt() throws Exception {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() throws Exception {
        return Long.parseLong(nextToken());
    }

    public double nextDouble() throws Exception {
        return Double.parseDouble(nextToken());
    }
}


import java.awt.Point;
import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.ObjectInputStream.GetField;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.security.KeyStore.Entry;
import java.text.DecimalFormat;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.Formatter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentMap;

public class Solution {
    static FastReader in = new FastReader(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter out = new PrintWriter(System.out);
    static boolean file = false;
    static final int maxn = (int)1e5+111;
    static int inf = (int)1e9;
    static int n,m,d;
    static int cat[] = new int[maxn];
    static int deg[] = new int[maxn];
    static int ans[] = new int[maxn];
    static ArrayList<Integer> a[] = new ArrayList[maxn];

    private static void solve() throws Exception {
        n = in.nextInt();
        m = in.nextInt();

        for (int i=1; i<=n; i++) {
            a[i] = new ArrayList<Integer>();
            ans[i] = -1;
            cat[i] = in.nextInt();
        }

        for (int i=1; i<n; i++) {
            int from = in.nextInt();
            int to = in.nextInt();
            deg[from]++;
            deg[to]++;
            a[from].add(to);
            a[to].add(from);
        }

        rec(1,cat[1], 0);
        int cnt=0;
        for (int i=2; i<=n; i++) {
            if (deg[i]==1) {
                if (ans[i]!=-1) {
                    if (ans[i]<=m) {
                        cnt++;
                    }
                }
            }
        }

        out.println(cnt);
    }

    public static void rec(int v, int sum, int p) {
        if (sum>m) return;
        ans[v] = sum;
        for (int i=0; i<a[v].size(); i++) {
            int to = a[v].get(i);
            if (to==p) continue;
            if (cat[to]==0) {
                rec(to,0,v);
            } else {
                rec(to,sum+1,v);
            }
        }
    }

    public static void main (String [] args) throws Exception {

        if (file) {
            in = new FastReader(new BufferedReader(new FileReader("input.txt")));
            out = new PrintWriter ("output.txt");
        }

        solve();
        out.close();
    }
}

class Pair implements Comparable<Pair> {
    int x,y;

    public Pair (int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public int compareTo(Pair p) {
        if (x>p.x) return 1;
        else if (x==p.x) return 0;
        else return -1;
    }
}

class FastReader {
    BufferedReader bf;
    StringTokenizer tk = null;

    public FastReader(BufferedReader bf) {
        this.bf = bf;
    }

    public String nextToken () throws Exception {
        if (tk==null || !tk.hasMoreTokens()) {
            tk = new StringTokenizer(bf.readLine());
        }
        return tk.nextToken();
    }

    public int nextInt() throws Exception {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() throws Exception {
        return Long.parseLong(nextToken());
    }

    public double nextDouble() throws Exception {
        return Double.parseDouble(nextToken());
    }
}


import java.math.*;
import java.util.*;

public class Main {
	static Edge e[];
	static int[] stat,head;
	static int sz,m;
	static void addedge(int u,int v){
		e[sz] = new Edge();
		e[sz].to = v;
		e[sz].next = head[u];
		head[u] = sz ++;
	}
	static int dfs(int u,int f,int cur){
		int res = 0,leaf = 1;
		for(int i = head[u] ; i != -1 ; i = e[i].next){
			int v = e[i].to;
			if(v == f) continue;
			leaf = 0;
			res += dfs(v,u,cur + stat[v]);
		}
		if(cur > m) return leaf;
		return res + leaf;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int n,x,y;
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext()){
			n = cin.nextInt();m = cin.nextInt();
			e = new Edge[n * 2 + 5];
			stat = new int[n + 5];
			head = new int[n + 5];
			sz = 0;
			for(int i = 1 ; i <= n ; i ++){
				stat[i] = cin.nextInt();
				head[i] = -1;
			}
			for(int i = 1 ; i < n ; i ++){
				x = cin.nextInt();
				y = cin.nextInt();
				addedge(x,y);
				addedge(y,x);
			}
			System.out.println(dfs(1,0,stat[1]));
		}
		cin.close();
	}
}

class Edge{
	public int to,next;
}


import java.math.*;

public class Main {
	static Edge e[];
	static int[] stat,head;
	static int sz,m;
	static void addedge(int u,int v){
		e[sz] = new Edge();
		e[sz].to = v;
		e[sz].next = head[u];
		head[u] = sz ++;
	}
	static int dfs(int u,int f,int cur){
		int res = 0,leaf = 1;
		for(int i = head[u] ; i != -1 ; i = e[i].next){
			int v = e[i].to;
			if(v == f) continue;
			leaf = 0;
			res += dfs(v,u,cur + stat[v]);
		}
		if(cur > m) return leaf;
		return res + leaf;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int n,x,y;
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext()){
			n = cin.nextInt();m = cin.nextInt();
			e = new Edge[n * 2 + 5];
			stat = new int[n + 5];
			head = new int[n + 5];
			sz = 0;
			for(int i = 1 ; i <= n ; i ++){
				stat[i] = cin.nextInt();
				head[i] = -1;
			}
			for(int i = 1 ; i < n ; i ++){
				x = cin.nextInt();
				y = cin.nextInt();
				addedge(x,y);
				addedge(y,x);
			}
			System.out.println(dfs(1,0,stat[1]));
		}
		cin.close();
	}
}

class Edge{
	public int to,next;
}


import java.math.*;
import java.util.*;

public class Main {
	static Edge e[];
	static int[] stat,head;
	static int sz,m;
	static void addedge(int u,int v){
		e[sz] = new Edge();
		e[sz].to = v;
		e[sz].next = head[u];
		head[u] = sz ++;
	}
	static int dfs(int u,int f,int cur){
		int res = 0,leaf = 1;
		int ncur = stat[u] != 0 ? cur + 1 : 0;
		if(ncur > m) return 0;
		for(int i = head[u] ; i != -1 ; i = e[i].next){
			int v = e[i].to;
			if(v == f) continue;
			leaf = 0;
			res += dfs(v,u,ncur);
		}
		return res + leaf;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int n,x,y;
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext()){
			n = cin.nextInt();m = cin.nextInt();
			e = new Edge[n * 2 + 5];
			stat = new int[n + 5];
			head = new int[n + 5];
			sz = 0;
			for(int i = 1 ; i <= n ; i ++){
				stat[i] = cin.nextInt();
				head[i] = -1;
			}
			for(int i = 1 ; i < n ; i ++){
				x = cin.nextInt();
				y = cin.nextInt();
				addedge(x,y);
				addedge(y,x);
			}
			System.out.println(dfs(1,0,0));
		}
		cin.close();
	}
}

class Edge{
	public int to,next;
}


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
/*
	  br = new BufferedReader(new FileReader("input.txt"));
	  pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
	  br = new BufferedReader(new InputStreamReader(System.in));
	  pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
 */

public class Main {
	private static BufferedReader br;
	private static StringTokenizer st;
	private static PrintWriter pw;

	public static void main(String[] args) throws Exception {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		//int qq = 1;
		int qq = Integer.MAX_VALUE;
		//int qq = readInt();
		for(int casenum = 1; casenum <= qq; casenum++)	{
			int n = readInt();
			int k = readInt();
			boolean[] is = new boolean[n];
			for(int i = 0; i < n; i++) {
				is[i] = readInt() == 1;
			}
			LinkedList<Integer>[] edges = new LinkedList[n];
			for(int i = 0; i < n; i++) {
				edges[i] = new LinkedList<Integer>();
			}
			for(int a = 1; a < n; a++) {
				int x = readInt()-1;
				int y = readInt()-1;
				edges[x].add(y);
				edges[y].add(x);
			}
			int[] p = new int[n];
			Arrays.fill(p, -1);
			p[0] = -2;
			LinkedList<Integer> q = new LinkedList<Integer>();
			int[] dp = new int[n];
			boolean[] seen = new boolean[n];
			dp[0] = is[0] ? 1 : 0;
			q.add(0);
			seen[0] = true;
			while(!q.isEmpty()) {
				int curr = q.removeFirst();
				for(int out: edges[curr]) {
					if(p[out] == -1) {
						if(dp[curr] == k && is[out]) continue;
						if(is[out]) {
							dp[out] = dp[curr]+1;
						}
						else {
							dp[out] = 0;
						}
						q.add(out);
						seen[out] = true;
						p[out] = curr;
					}
				}
			}
			int ret = 0;
			for(int i = 0; i < n; i++) {
				if(seen[i] && edges[i].size() == 1 && p[i] >= 0) {
					ret++;
				}
			}
			pw.println(ret);
		}
		exitImmediately();
	}

	private static void exitImmediately() {
		pw.close();
		System.exit(0);
	}

	private static long readLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	private static double readDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	private static int readInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	private static String nextLine() throws IOException  {
		if(!br.ready()) {
			exitImmediately();
		}
		st = null;
		return br.readLine();
	}

	private static String nextToken() throws IOException  {
		while(st == null || !st.hasMoreTokens())  {
			if(!br.ready()) {
				exitImmediately();
			}
			st = new StringTokenizer(br.readLine().trim());
		}
		return st.nextToken();
	}
}


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
/*
	  br = new BufferedReader(new FileReader("input.txt"));
	  pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
	  br = new BufferedReader(new InputStreamReader(System.in));
	  pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
 */

public class Main {
	private static BufferedReader br;
	private static StringTokenizer st;
	private static PrintWriter pw;

	public static void main(String[] args) throws Exception {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		//int qq = 1;
		int qq = Integer.MAX_VALUE;
		//int qq = readInt();
		for(int casenum = 1; casenum <= qq; casenum++)	{
			int n = readInt();
			int k = readInt();
			boolean[] is = new boolean[n];
			for(int i = 0; i < n; i++) {
				is[i] = readInt() == 1;
			}
			LinkedList<Integer>[] edges = new LinkedList[n];
			for(int i = 0; i < n; i++) {
				edges[i] = new LinkedList<Integer>();
			}
			for(int a = 1; a < n; a++) {
				int x = readInt()-1;
				int y = readInt()-1;
				edges[x].add(y);
				edges[y].add(x);
			}
			int[] p = new int[n];
			Arrays.fill(p, -1);
			p[0] = -2;
			LinkedList<Integer> q = new LinkedList<Integer>();
			int[] dp = new int[n];
			boolean[] seen = new boolean[n];
			dp[0] = is[0] ? 1 : 0;
			q.add(0);
			seen[0] = true;
			while(!q.isEmpty()) {
				int curr = q.removeFirst();
				for(int out: edges[curr]) {
					if(p[out] >= 0) continue;
					if(dp[curr] == k && is[out]) continue;
					if(is[out]) {
						dp[out] = dp[curr]+1;
					}
					else {
						dp[out] = 0;
					}
					q.add(out);
					seen[out] = true;
					p[out] = curr;
				}
			}
			int ret = 0;
			for(int i = 0; i < n; i++) {
				if(seen[i] && edges[i].size() == 1 && p[i] >= 0) {
					ret++;
				}
			}
			pw.println(ret);
		}
		exitImmediately();
	}

	private static void exitImmediately() {
		pw.close();
		System.exit(0);
	}

	private static long readLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	private static double readDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	private static int readInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	private static String nextLine() throws IOException  {
		if(!br.ready()) {
			exitImmediately();
		}
		st = null;
		return br.readLine();
	}

	private static String nextToken() throws IOException  {
		while(st == null || !st.hasMoreTokens())  {
			if(!br.ready()) {
				exitImmediately();
			}
			st = new StringTokenizer(br.readLine().trim());
		}
		return st.nextToken();
	}
}


package mypackage;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.PrintWriter;

public class TaskC {
    public void solve(int testNumber, Scanner in, PrintWriter out) {
        int n = in.nextInt();
        int maxCat = in.nextInt();
        TreeNode[] nodes = new TreeNode[n];
        for (int i = 0; i < n; i++) {
            TreeNode treeNode = new TreeNode(in.nextInt() == 1);
            treeNode.setIndex(i);
            nodes[i] = treeNode;
        }
        for (int i = 0; i < n - 1; i++) {
            int x = in.nextInt() - 1;
            int y = in.nextInt() - 1;
            nodes[x].addChild(nodes[y]);
            nodes[y].addChild(nodes[x]);
        }

        List<TreeNode> toProcess = new ArrayList<>();
        if (nodes[0].hasCat) {
            nodes[0].cats = 1;
        }
        toProcess.add(nodes[0]);
        int count = 0;
        while (toProcess.size() > 0) {
            TreeNode curNode = toProcess.remove(0);
            curNode.processed = true;
            for (TreeNode node : curNode.children) {
                node.children.remove(curNode);
                if (curNode.cats > 0 && node.hasCat) {
                    node.cats = curNode.cats + 1;
                } else if (node.hasCat) {
                    node.cats = 1;
                } else {
                    node.cats = 0;
                }
                if (node.cats <= maxCat && curNode.cats <= maxCat) {
                    toProcess.add(node);
                }
            }
            if (curNode.children.size() == 0) {
                count++;
            }
        }
        out.println(count);
    }

    class TreeNode {
        boolean hasCat;
        List<TreeNode> children;
        public boolean processed = false;
        public int cats = 0;
        int index;

        public TreeNode(boolean hasCat) {
            this.hasCat = hasCat;
            children = new ArrayList<>();
        }

        public void addChild(TreeNode treeNode) {
            children.add(treeNode);
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }
    }
}


package mypackage;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.PrintWriter;

public class TaskC {
    public void solve(int testNumber, Scanner in, PrintWriter out) {
        int n = in.nextInt();
        int maxCat = in.nextInt();
        TreeNode[] nodes = new TreeNode[n];
        for (int i = 0; i < n; i++) {
            nodes[i] = new TreeNode(in.nextInt() == 1);
        }
        for (int i = 0; i < n - 1; i++) {
            nodes[in.nextInt() - 1].addChild(nodes[in.nextInt() - 1]);
        }

        List<TreeNode> toProcess = new ArrayList<>();
        if (nodes[0].hasCat) {
            nodes[0].cats = 1;
        }
        toProcess.add(nodes[0]);
        int count = 0;
        while (toProcess.size() > 0) {
            TreeNode curNode = toProcess.remove(0);
            for (TreeNode node : curNode.children) {
                if (curNode.cats > 0 && node.hasCat) {
                    node.cats = curNode.cats + 1;
                } else if (node.hasCat) {
                    node.cats = 1;
                } else {
                    node.cats = 0;
                }
                if (node.cats <= maxCat) {
                    toProcess.add(node);
                }
            }
            if (curNode.children.size() == 0) {
                count++;
            }
        }
        out.println(count);
    }

    class TreeNode {
        boolean hasCat;
        List<TreeNode> children;
        TreeNode parent;
        public int cats = 0;

        public TreeNode(boolean hasCat) {
            this.hasCat = hasCat;
            children = new ArrayList<>();
        }

        public void addChild(TreeNode treeNode) {
            children.add(treeNode);
            treeNode.parent = this;
        }

        public void setParent(TreeNode parent) {
            this.parent = parent;
        }
    }
}


import java.util.List;
import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskC {
    public void solve(int testNumber, Scanner in, PrintWriter out) {
        int n = in.nextInt();
        int maxCat = in.nextInt();
        TreeNode[] nodes = new TreeNode[n];
        for (int i = 0; i < n; i++) {
            nodes[i] = new TreeNode(in.nextInt() == 1);
        }
        for (int i = 0; i < n - 1; i++) {
            nodes[in.nextInt() - 1].addChild(nodes[in.nextInt() - 1]);
        }

        List<TreeNode> toProcess = new ArrayList<>();
        if (nodes[0].hasCat) {
            nodes[0].cats = 1;
        }
        toProcess.add(nodes[0]);
        int count = 0;
        while (toProcess.size() > 0) {
            TreeNode curNode = toProcess.remove(0);
            for (TreeNode node : curNode.children) {
                if (curNode.cats > 0 && node.hasCat) {
                    node.cats = curNode.cats + 1;
                } else if (node.hasCat) {
                    node.cats = 1;
                } else {
                    node.cats = 0;
                }
                if (node.cats <= maxCat) {
                    toProcess.add(node);
                }
            }
            if (curNode.children.size() == 0) {
                count++;
            }
        }
        out.println(count);
    }

    class TreeNode {
        boolean hasCat;
        List<TreeNode> children;
        TreeNode parent;
        public int cats = 0;

        public TreeNode(boolean hasCat) {
            this.hasCat = hasCat;
            children = new ArrayList<>();
        }

        public void addChild(TreeNode treeNode) {
            children.add(treeNode);
            treeNode.parent = this;
        }

        public void setParent(TreeNode parent) {
            this.parent = parent;
        }
    }
}


import java.util.List;
import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskC {
    public void solve(int testNumber, Scanner in, PrintWriter out) {
        int n = in.nextInt();
        int maxCat = in.nextInt();
        TreeNode[] nodes = new TreeNode[n];
        for (int i = 0; i < n; i++) {
            TreeNode treeNode = new TreeNode(in.nextInt() == 1);
            treeNode.setIndex(i);
            nodes[i] = treeNode;
        }
        for (int i = 0; i < n - 1; i++) {
            int x = in.nextInt() - 1;
            int y = in.nextInt() - 1;
            nodes[x].addChild(nodes[y]);
            nodes[y].addChild(nodes[x]);
        }

        List<TreeNode> toProcess = new ArrayList<>();
        if (nodes[0].hasCat) {
            nodes[0].cats = 1;
        }
        toProcess.add(nodes[0]);
        int count = 0;
        while (toProcess.size() > 0) {
            TreeNode curNode = toProcess.remove(0);
            curNode.processed = true;
            for (TreeNode node : curNode.children) {
                node.children.remove(curNode);
                if (curNode.cats > 0 && node.hasCat) {
                    node.cats = curNode.cats + 1;
                } else if (node.hasCat) {
                    node.cats = 1;
                } else {
                    node.cats = 0;
                }
                if (node.cats <= maxCat && curNode.cats <= maxCat) {
                    toProcess.add(node);
                }
            }
            if (curNode.children.size() == 0) {
                count++;
            }
        }
        out.println(count);
    }

    class TreeNode {
        boolean hasCat;
        List<TreeNode> children;
        public boolean processed = false;
        public int cats = 0;
        int index;

        public TreeNode(boolean hasCat) {
            this.hasCat = hasCat;
            children = new ArrayList<>();
        }

        public void addChild(TreeNode treeNode) {
            children.add(treeNode);
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }
    }
}


import java.util.List;
import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskC {
    public void solve(int testNumber, Scanner in, PrintWriter out) {
        int n = in.nextInt();
        int maxCat = in.nextInt();
        TreeNode[] nodes = new TreeNode[n];
        for (int i = 0; i < n; i++) {
            nodes[i] = new TreeNode(in.nextInt() == 1);
        }
        for (int i = 0; i < n - 1; i++) {
            nodes[in.nextInt() - 1].addChild(nodes[in.nextInt() - 1]);
        }

        List<TreeNode> toProcess = new ArrayList<>();
        if (nodes[0].hasCat) {
            nodes[0].cats = 1;
        }
        toProcess.add(nodes[0]);
        int count = 0;
        while (toProcess.size() > 0) {
            TreeNode curNode = toProcess.remove(0);
            for (TreeNode node : curNode.children) {
                if (curNode.cats > 0 && node.hasCat) {
                    node.cats = curNode.cats + 1;
                } else if (node.hasCat) {
                    node.cats = 1;
                } else {
                    node.cats = 0;
                }
                if (node.cats <= maxCat && curNode.cats<=maxCat) {
                    toProcess.add(node);
                }
            }
            if (curNode.children.size() == 0) {
                count++;
            }
        }
        out.println(count);
    }

    class TreeNode {
        boolean hasCat;
        List<TreeNode> children;
        TreeNode parent;
        public int cats = 0;

        public TreeNode(boolean hasCat) {
            this.hasCat = hasCat;
            children = new ArrayList<>();
        }

        public void addChild(TreeNode treeNode) {
            children.add(treeNode);
            treeNode.parent = this;
        }

        public void setParent(TreeNode parent) {
            this.parent = parent;
        }
    }
}


//package solution;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;

public class Solution implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");
    final boolean OJ = System.getProperty("ONLINE_JUDGE") != null;

    void init() throws FileNotFoundException {
        if (OJ) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        }

    }

    public void run() {
        try {
            init();
            solve();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }

        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    public static void main(String[] args) {
        new Thread(null, new Solution(), "", 256 * 1l << 20).start();
    }

    ArrayList<Integer>[] graph;
    int[] hasCat;
    int m;
    int n;
    int ans = 0;

    void dfs(int v, int prev, int parent) {
        if (hasCat[v] != 0) prev += hasCat[v];
        else prev = 0;
        if (prev > m) return;
        if (graph[v].size() == 0) ans++;
        for (int to : graph[v]) {
            if (to != parent) dfs(to, prev, v);
        }
    }

    void solve() throws IOException {
        n = readInt();
        m = readInt();
        graph = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            graph[i] = new ArrayList<>();
        }
        hasCat = new int[n];
        for (int i = 0; i < n; i++) {
            hasCat[i] = readInt();
        }

        for (int i = 0; i < n - 1; i++) {
            int from = readInt();
            int to = readInt();
            graph[from - 1].add(to - 1);
            graph[to - 1].add(from - 1);
        }
        dfs(0, 0, -1);
        out.print(ans);

    }
}


//package solution;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;

public class Solution implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");
    final boolean OJ = System.getProperty("ONLINE_JUDGE") != null;

    void init() throws FileNotFoundException {
        if (OJ) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        }

    }

    public void run() {
        try {
            init();
            solve();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }

        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    public static void main(String[] args) {
        new Thread(null, new Solution(), "", 256 * 1l << 20).start();
    }

    ArrayList<Integer>[] graph;
    int[] hasCat;
    int m;
    int n;
    int ans = 0;

    void dfs(int v, int prev) {
        prev += hasCat[v];
        if (prev > m) return;
        if (graph[v].size() == 0) ans++;
        for (int to : graph[v]) {
            dfs(to, prev);
        }
    }

    void solve() throws IOException {
        n = readInt();
        m = readInt();
        graph = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            graph[i] = new ArrayList<>();
        }
        hasCat = new int[n];
        for (int i = 0; i < n; i++) {
            hasCat[i] = readInt();
        }

        for (int i = 0; i < n - 1; i++) {
            int from = readInt();
            int to = readInt();
            graph[from - 1].add(to - 1);
        }
        dfs(0, 0);
        out.print(ans);

    }
}


//package solution;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;

public class Solution implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");
    final boolean OJ = System.getProperty("ONLINE_JUDGE") != null;

    void init() throws FileNotFoundException {
        if (OJ) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        }

    }

    public void run() {
        try {
            init();
            solve();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }

        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    public static void main(String[] args) {
        new Thread(null, new Solution(), "", 256 * 1l << 20).start();
    }

    ArrayList<Integer>[] graph;
    int[] hasCat;
    int m;
    int n;
    int ans = 0;

    void dfs(int v, int prev, int parent) {
        //out.println(v + " " + prev + " " + parent);
        if (hasCat[v] != 0) prev += hasCat[v];
        else prev = 0;
        if (prev > m) return;
        if (graph[v].size() == 1 && v != 0) ans++;
        for (int to : graph[v]) {
            if (to != parent) dfs(to, prev, v);
        }
    }

    void solve() throws IOException {
        n = readInt();
        m = readInt();
        graph = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            graph[i] = new ArrayList<>();
        }
        hasCat = new int[n];
        for (int i = 0; i < n; i++) {
            hasCat[i] = readInt();
        }

        for (int i = 0; i < n - 1; i++) {
            int from = readInt();
            int to = readInt();
            graph[from - 1].add(to - 1);
            graph[to - 1].add(from - 1);
        }
        dfs(0, 0, -1);
        out.print(ans);

    }
}


package solution;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;

public class Solution implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");
    final boolean OJ = System.getProperty("ONLINE_JUDGE") != null;

    void init() throws FileNotFoundException {
        if (OJ) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        }

    }

    public void run() {
        try {
            init();
            solve();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }

        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    public static void main(String[] args) {
        new Thread(null, new Solution(), "", 256 * 1l << 20).start();
    }

    ArrayList<Integer>[] graph;
    int[] hasCat;
    int m;
    int n;
    int ans = 0;

    void dfs(int v, int prev, int parent) {
        //out.println(v + " " + prev + " " + parent);
        if (hasCat[v] != 0) prev += hasCat[v];
        else prev = 0;
        if (prev > m) return;
        if (graph[v].size() == 1 && v != 0) ans++;
        for (int to : graph[v]) {
            if (to != parent) dfs(to, prev, v);
        }
    }

    void solve() throws IOException {
        n = readInt();
        m = readInt();
        graph = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            graph[i] = new ArrayList<>();
        }
        hasCat = new int[n];
        for (int i = 0; i < n; i++) {
            hasCat[i] = readInt();
        }

        for (int i = 0; i < n - 1; i++) {
            int from = readInt();
            int to = readInt();
            graph[from - 1].add(to - 1);
            graph[to - 1].add(from - 1);
        }
        dfs(0, 0, -1);
        out.print(ans);

    }
}


//package solution;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;

public class Solution implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");
    final boolean OJ = System.getProperty("ONLINE_JUDGE") != null;

    void init() throws FileNotFoundException {
        if (OJ) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        }

    }

    public void run() {
        try {
            init();
            solve();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }

        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    public static void main(String[] args) {
        new Thread(null, new Solution(), "", 256 * 1l << 20).start();
    }

    ArrayList<Integer>[] graph;
    int[] hasCat;
    int m;
    int n;
    int ans = 0;

    void dfs(int v, int prev) {
        prev += hasCat[v];
        if (prev > m) return;
        if (graph[v].size() == 0) ans++;
        for (int to : graph[v]) {
            dfs(to, prev);
        }
    }

    void solve() throws IOException {
        n = readInt();
        m = readInt();
        graph = new ArrayList[n];
        hasCat = new int[n];
        for (int i = 0; i < n; i++) {
            hasCat[i] = readInt();
        }

        for (int i = 0; i < n - 1; i++) {
            int from = readInt();
            int to = readInt();
            graph[from - 1].add(to - 1);
        }
        dfs(0, 0);
        out.print(ans);

    }
}


//package solution;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;

public class Solution implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");
    final boolean OJ = System.getProperty("ONLINE_JUDGE") != null;

    void init() throws FileNotFoundException {
        if (OJ) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        }

    }

    public void run() {
        try {
            init();
            solve();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }

        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    public static void main(String[] args) {
        new Thread(null, new Solution(), "", 256 * 1l << 20).start();
    }

    ArrayList<Integer>[] graph;
    int[] hasCat;
    int m;
    int n;
    int ans = 0;

    void dfs(int v, int prev) {
        if (hasCat[v] != 0) prev += hasCat[v];
        else prev = 0;
        if (prev > m) return;
        if (graph[v].size() == 0) ans++;
        for (int to : graph[v]) {
            dfs(to, prev);
        }
    }

    void solve() throws IOException {
        n = readInt();
        m = readInt();
        graph = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            graph[i] = new ArrayList<>();
        }
        hasCat = new int[n];
        for (int i = 0; i < n; i++) {
            hasCat[i] = readInt();
        }

        for (int i = 0; i < n - 1; i++) {
            int from = readInt();
            int to = readInt();
            graph[from - 1].add(to - 1);
        }
        dfs(0, 0);
        out.print(ans);

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        List<Integer>[] g;
        boolean[] haveCat;
        boolean[] was;
        int m;
        int result;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            m = in.nextInt();
            haveCat = new boolean[n];
            g = new List[n];
            for (int i = 0; i < n; i++) {
                haveCat[i] = in.nextInt() == 1;
                g[i] = new ArrayList<Integer>();
            }
            for (int i = 0; i < n - 1; i++) {
                int u = in.nextInt() - 1;
                int v = in.nextInt() - 1;
                g[u].add(v);
                g[v].add(u);
            }
            was = new boolean[n];
            result = 0;
            dfs(0, 0, 0);
            out.println(result);
        }

        void dfs(int u, int maxCat, int currentCat) {
            was[u] = true;
            if (haveCat[u]) {
                ++currentCat;
            } else {
                currentCat = 0;
            }
            maxCat = Math.max(maxCat, currentCat);
            boolean found = false;
            for (int v : g[u]) {
                if (!was[v]) {
                    found = true;
                    dfs(v, maxCat, currentCat);
                }
            }
            if (!found && maxCat <= m) {
                ++result;
            }
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Solution
{
    public static boolean[] cats;
    public static boolean[] visited;
    public static LinkedList<Integer>[] adj;

    public static int dfs(int top ,int count,int m){
        if(visited[top])return 0;
        if(cats[top])count++;
        else count = 0;
        visited[top] = true;
        if(count > m)return 0;
        if(adj[top].size() == 1 && top!=0)return 1;
        int ans = 0;
        for(int  i : adj[top]){
            ans += dfs(i,count,m);
        }
        return ans;
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
         cats = new boolean[n];
         visited = new boolean[n];
        Arrays.fill(visited,false);
        Arrays.fill(cats,false);
        for(int  i=0;i<n;i++){
            cats[i] = sc.nextInt() == 1;
        }
         adj = new LinkedList[n];
        for(int i =0;i<n;i++)adj[i] = new LinkedList<Integer>();
        for(int i =0;i<n-1;i++){
            int x = sc.nextInt(),y = sc.nextInt();
            adj[x-1].add(y-1);
            adj[y-1].add(x-1);
        }
        int ans = dfs(0,0,m);
        System.out.println(ans);
    }
}


/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Solution
{
    public static boolean[] cats;
    public static boolean[] visited;
    public static LinkedList<Integer>[] adj;

    public static int dfs(int top ,int count,int m){
        if(visited[top])return 0;
        if(cats[top])count++;
        else count = 0;
        visited[top] = true;
        if(count > m)return 0;
        if(adj[top].size() == 1)return 1;
        int ans = 0;
        for(int  i : adj[top]){
            ans += dfs(i,count,m);
        }
        return ans;
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
         cats = new boolean[n];
         visited = new boolean[n];
        Arrays.fill(visited,false);
        Arrays.fill(cats,false);
        for(int  i=0;i<n;i++){
            cats[i] = sc.nextInt() == 1;
        }
         adj = new LinkedList[n];
        for(int i =0;i<n;i++)adj[i] = new LinkedList<Integer>();
        for(int i =0;i<n-1;i++){
            int x = sc.nextInt(),y = sc.nextInt();
            adj[x-1].add(y-1);
            adj[y-1].add(x-1);
        }
        int ans = dfs(0,0,m);
        System.out.println(ans);
    }
}


/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Solution
{
    public static boolean[] cats;
    public static boolean[] visited;
    public static LinkedList<Integer>[] adj;

    public static int dfs(int top ,int count,int m){
        if(visited[top])return 0;
        if(cats[top])count++;
        else count = 0;
        if(count > m)return 0;
        if(adj[top].size() == 1)return 1;
        int ans = 0;
        for(int  i : adj[top]){
            ans += dfs(i,count,m);
        }
        return ans;
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
         cats = new boolean[n];
         visited = new boolean[n];
        Arrays.fill(visited,false);
        Arrays.fill(cats,false);
        for(int  i=0;i<n;i++){
            cats[i] = sc.nextInt() == 1;
        }
         adj = new LinkedList[n];
        for(int i =0;i<n;i++)adj[i] = new LinkedList<Integer>();
        for(int i =0;i<n-1;i++){
            int x = sc.nextInt(),y = sc.nextInt();
            adj[x-1].add(y-1);
            adj[y-1].add(x-1);
        }
        int ans = dfs(0,0,m);
        System.out.println(ans);
    }
}


import java.util.ArrayList;
import java.util.Scanner;

public class Solve {

    int DFS(int cur, int[] labels, ArrayList[] adj, int catNum, int m) {
        if (labels[cur] == 1)
            catNum++;
        else
            catNum = 0;
        if (catNum > m)
            return 0;
        if (adj[cur] == null || adj[cur].isEmpty())
            return 1;
        int res = 0;
        for (Object i : adj[cur]) {
            res += DFS((Integer) i, labels, adj, catNum, m);
        }
        return res;
    }

    ArrayList[] Construct(int[][] edges, int n) {
        ArrayList[] adj = new ArrayList[n];
        for (int i = 0; i < n - 1; i++) {
            int parent = edges[i][0];
            if (adj[parent] == null)
                adj[parent] = new ArrayList<Integer>();
            adj[parent].add(edges[i][1]);
        }
        return adj;
    }

    public static void main(String[] args) {
        Solve s = new Solve();
        // int n = 7, m = 1;
        // int labels[] = new int[] {1, 0, 1, 1, 0, 0, 0};
        // int[][] edges = new int[][] { {0, 1}, {0, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}};
        // ArrayList[] adj = s.Construct(edges, n);
        // System.out.println(s.DFS(0, labels, adj, 0, m));

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int labels[] = new int[n];
        for (int i = 0; i < n; i++)
            labels[i] = sc.nextInt();
        int[][] edges = new int[n - 1][2];
        for (int i = 0; i < n - 1; i++) {
            edges[i][0] = sc.nextInt() - 1;
            edges[i][1] = sc.nextInt() - 1;
        }
        ArrayList[] adj = s.Construct(edges, n);
        System.out.println(s.DFS(0, labels, adj, 0, m));
    }
}


import java.util.ArrayList;
import java.util.Scanner;

public class Solve {

    int DFS(int cur, int[] labels, ArrayList[] adj, int catNum, int m) {
        if (labels[cur] == 1)
            catNum++;
        if (catNum > m)
            return 0;
        if (adj[cur] == null || adj[cur].isEmpty())
            return 1;
        int res = 0;
        for (Object i : adj[cur]) {
            res += DFS((Integer) i, labels, adj, catNum, m);
        }
        return res;
    }

    ArrayList[] Construct(int[][] edges, int n) {
        ArrayList[] adj = new ArrayList[n];
        for (int i = 0; i < n - 1; i++) {
            int parent = edges[i][0];
            if (adj[parent] == null)
                adj[parent] = new ArrayList<Integer>();
            adj[parent].add(edges[i][1]);
        }
        return adj;
    }

    public static void main(String[] args) {
        Solve s = new Solve();
        // int n = 7, m = 1;
        // int labels[] = new int[] {1, 0, 1, 1, 0, 0, 0};
        // int[][] edges = new int[][] { {0, 1}, {0, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}};
        // ArrayList[] adj = s.Construct(edges, n);
        // System.out.println(s.DFS(0, labels, adj, 0, m));

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int labels[] = new int[n];
        for (int i = 0; i < n; i++)
            labels[i] = sc.nextInt();
        int[][] edges = new int[n - 1][2];
        for (int i = 0; i < n - 1; i++) {
            edges[i][0] = sc.nextInt();
            edges[i][1] = sc.nextInt();
        }
        ArrayList[] adj = s.Construct(edges, n);
        System.out.println(s.DFS(0, labels, adj, 0, m));
    }
}


import java.util.ArrayList;
import java.util.Scanner;

public class Solve {

    int DFS(int cur, int[] labels, ArrayList[] adj, boolean visited[], int catNum, int m) {
        if (labels[cur] == 1)
            catNum++;
        else
            catNum = 0;
        if (catNum > m)
            return 0;
        int res = 0;
        visited[cur] = true;
        boolean has = false;
        for (Object i : adj[cur]) {
            if (!visited[(Integer) i]) {
                res += DFS((Integer) i, labels, adj, visited, catNum, m);
                has = true;
            }
        }
        if (!has)
            return 1;
        return res;
    }

    ArrayList[] Construct(int[][] edges, int n) {
        ArrayList[] adj = new ArrayList[n];
        for (int i = 0; i < n - 1; i++) {
            int parent = edges[i][0];
            if (adj[parent] == null)
                adj[parent] = new ArrayList<Integer>();
            adj[parent].add(edges[i][1]);
            parent = edges[i][1];
            if (adj[parent] == null)
                adj[parent] = new ArrayList<Integer>();
            adj[parent].add(edges[i][0]);
        }
        return adj;
    }

    public static void main(String[] args) {
        Solve s = new Solve();
        // int n = 7, m = 1;
        // int labels[] = new int[] {1, 0, 1, 1, 0, 0, 0};
        // int[][] edges = new int[][] { {0, 1}, {0, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}};
        // ArrayList[] adj = s.Construct(edges, n);
        // boolean visited[] = new boolean[n];
        // System.out.println(s.DFS(0, labels, adj, visited, 0, m));

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int labels[] = new int[n];
        for (int i = 0; i < n; i++)
            labels[i] = sc.nextInt();
        int[][] edges = new int[n - 1][2];
        for (int i = 0; i < n - 1; i++) {
            edges[i][0] = sc.nextInt() - 1;
            edges[i][1] = sc.nextInt() - 1;
        }
        ArrayList[] adj = s.Construct(edges, n);
        boolean visited[] = new boolean[n];
        System.out.println(s.DFS(0, labels, adj, visited, 0, m));
    }
}


import java.util.ArrayList;
import java.util.Scanner;

public class Solve {

    int DFS(int cur, int[] labels, ArrayList[] adj, int catNum, int m) {
        if (labels[cur] == 1)
            catNum++;
        if (catNum > m)
            return 0;
        if (adj[cur] == null || adj[cur].isEmpty())
            return 1;
        int res = 0;
        for (Object i : adj[cur]) {
            res += DFS((Integer) i, labels, adj, catNum, m);
        }
        return res;
    }

    ArrayList[] Construct(int[][] edges, int n) {
        ArrayList[] adj = new ArrayList[n];
        for (int i = 0; i < n - 1; i++) {
            int parent = edges[i][0];
            if (adj[parent] == null)
                adj[parent] = new ArrayList<Integer>();
            adj[parent].add(edges[i][1]);
        }
        return adj;
    }

    public static void main(String[] args) {
        Solve s = new Solve();
        // int n = 7, m = 1;
        // int labels[] = new int[] {1, 0, 1, 1, 0, 0, 0};
        // int[][] edges = new int[][] { {0, 1}, {0, 2}, {1, 3}, {1, 4}, {2, 5}, {2, 6}};
        // ArrayList[] adj = s.Construct(edges, n);
        // System.out.println(s.DFS(0, labels, adj, 0, m));

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int labels[] = new int[n];
        for (int i = 0; i < n; i++)
            labels[i] = sc.nextInt();
        int[][] edges = new int[n - 1][2];
        for (int i = 0; i < n - 1; i++) {
            edges[i][0] = sc.nextInt() - 1;
            edges[i][1] = sc.nextInt() - 1;
        }
        ArrayList[] adj = s.Construct(edges, n);
        System.out.println(s.DFS(0, labels, adj, 0, m));
    }
}


import java.util.*;

public class C {

	public static void main(String[] args) {

		Scanner qwe = new Scanner(System.in);

		int n = qwe.nextInt();
		int m = qwe.nextInt();

		int[] cats = new int[n];
		for (int i = 0; i < cats.length; i++) {
			cats[i] = qwe.nextInt();
		}

		ArrayList<Integer>[] aj = new ArrayList[n];
		for (int i = 0; i < aj.length; i++) {
			aj[i] = new ArrayList<Integer>();
		}
		for(int i =0; i < n-1; i++){
			int a = qwe.nextInt()-1;
			int b = qwe.nextInt()-1;
			aj[a].add(b);
			aj[b].add(a);
		}

		ArrayDeque<Integer> dq = new ArrayDeque<Integer>();
		boolean[] been = new boolean[n];
		been[0] = true;
		dq.add(0);
		dq.add(cats[0]);
		int ans = 0;
		while(!dq.isEmpty()){
			int at = dq.poll();
			int conseq = dq.poll();

			boolean isleaf = true;
			for(int to : aj[at]){
				if(!been[to]){
					been[to] = true;
					isleaf = false;
					int hascat = cats[to];
					if(conseq+hascat > m){
						continue;
					}
					else{
						dq.add(to);
						dq.add(hascat == 1 ? conseq+1 :  0);
					}
				}
			}

			if(isleaf) ans++;

		}

		System.out.println(ans);

		qwe.close();
	}

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastInputReader in = new FastInputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        boolean[] vis;

        public void solve(int testNumber, FastInputReader in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] cats = new int[n];
            List<Integer>[] graph = new List[n];
            for (int i = 0; i < n; i++) {
                graph[i] = new ArrayList<>();
                cats[i] = in.nextInt();
            }

            for (int i = 0; i < n - 1; i++) {
                int x = in.nextInt() - 1;
                int y = in.nextInt() - 1;
                graph[x].add(y);
                graph[y].add(x);
            }
            vis = new boolean[n];
            int ans = dfs(0, 0, graph, cats, m);
            out.println(ans);
        }

        private int dfs(int cur, int count, List<Integer>[] graph, int[] cats, int m) {
            vis[cur] = true;
            int res = 0;
            if (count + cats[cur] > m) {
                return 0;
            }
            if (cur != 0 && graph[cur].size() == 1)
                return 1;
            for (int next : graph[cur]) {
                if (!vis[next]) {
                    if (cats[cur] == 1) {
                        res += dfs(next, count + 1, graph, cats, m);
                    } else {
                        res += dfs(next, 0, graph, cats, m);
                    }
                }
            }
            return res;
        }

    }

    static class FastInputReader {
        static InputStream is;
        static private byte[] buffer;
        static private int lenbuf;
        static private int ptrbuf;

        public FastInputReader(InputStream stream) {
            is = stream;
            buffer = new byte[1024];
            lenbuf = 0;
            ptrbuf = 0;
        }

        private int readByte() {
            if (lenbuf == -1)
                throw new InputMismatchException();
            if (ptrbuf >= lenbuf) {
                ptrbuf = 0;
                try {
                    lenbuf = is.read(buffer);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (lenbuf <= 0)
                    return -1;
            }
            return buffer[ptrbuf++];
        }

        public int nextInt() {
            int num = 0, b;
            boolean minus = false;
            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
                ;
            if (b == '-') {
                minus = true;
                b = readByte();
            }

            while (true) {
                if (b >= '0' && b <= '9') {
                    num = num * 10 + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = readByte();
            }
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastInputReader in = new FastInputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        boolean[] vis;

        public void solve(int testNumber, FastInputReader in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] cats = new int[n];
            List<Integer>[] graph = new List[n];
            for (int i = 0; i < n; i++) {
                graph[i] = new ArrayList<>();
                cats[i] = in.nextInt();
            }

            for (int i = 0; i < n - 1; i++) {
                int x = in.nextInt() - 1;
                int y = in.nextInt() - 1;
                graph[x].add(y);
                graph[y].add(x);
            }
            vis = new boolean[n];
            int ans = dfs(0, 0, graph, cats, m);
            out.println(ans);
        }

        private int dfs(int cur, int count, List<Integer>[] graph, int[] cats, int m) {
            vis[cur] = true;
            int res = 0;
            if (count > m) {
                return 0;
            }
            if (cur != 1 && graph[cur].size() == 1)
                return 1;
            for (int next : graph[cur]) {
                if (!vis[next]) {
                    if (cats[cur] == 1) {
                        res += dfs(next, count + 1, graph, cats, m);
                    } else {
                        res += dfs(next, 0, graph, cats, m);
                    }
                }
            }
            return res;
        }

    }

    static class FastInputReader {
        static InputStream is;
        static private byte[] buffer;
        static private int lenbuf;
        static private int ptrbuf;

        public FastInputReader(InputStream stream) {
            is = stream;
            buffer = new byte[1024];
            lenbuf = 0;
            ptrbuf = 0;
        }

        private int readByte() {
            if (lenbuf == -1)
                throw new InputMismatchException();
            if (ptrbuf >= lenbuf) {
                ptrbuf = 0;
                try {
                    lenbuf = is.read(buffer);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (lenbuf <= 0)
                    return -1;
            }
            return buffer[ptrbuf++];
        }

        public int nextInt() {
            int num = 0, b;
            boolean minus = false;
            while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
                ;
            if (b == '-') {
                minus = true;
                b = readByte();
            }

            while (true) {
                if (b >= '0' && b <= '9') {
                    num = num * 10 + (b - '0');
                } else {
                    return minus ? -num : num;
                }
                b = readByte();
            }
        }

    }
}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ProblemC {

    static boolean[] hasCat;
    static boolean[] visited;
    static List<Integer>[] tree;
    static int result = 0;
    static int m;

    public static void dfs(int node, int currentSum, boolean afterCat){
        visited[node] = true;
        if (hasCat[node]){
            if (afterCat){
                currentSum++;
            }
            else {
                currentSum = 1;
            }
        }
        if (currentSum > m){
           return;
        }
        boolean isLeaf = true;
        for (int n: tree[node]){
            if (!visited[n]){
                isLeaf = false;
                dfs(n, currentSum, hasCat[node]);
            }
        }
        if (isLeaf){
            result++;
        }
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        m = sc.nextInt();
        hasCat = new boolean[n];
        visited = new boolean[n];
        for (int i = 0; i < n; i++){
            hasCat[i] = sc.nextInt() == 1;
        }
        tree = new List[n];
        for (int i = 0; i < n - 1; i++){
            int from = sc.nextInt() - 1;
            int to = sc.nextInt() - 1;
            if (tree[from] == null){
                tree[from] = new ArrayList<Integer>();
            }
            tree[from].add(to);
            if (tree[to] == null){
                tree[to] = new ArrayList<Integer>();
            }
            tree[to].add(from);
        }
        dfs(0, 0, false);
        System.out.println(result);
    }
}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ProblemC {

    static boolean[] hasCat;
    static List<Integer>[] tree;
    static int result = 0;
    static int m;

    public static void dfs(int node, int currentSum, boolean afterCat){
        if (hasCat[node]){
            if (afterCat){
                currentSum++;
            }
            else {
                currentSum = 1;
            }
        }
        if (currentSum > m){
           return;
        }
        if (tree[node] == null){
            result++;
            return;
        }
        for (int n: tree[node]){
            dfs(n, currentSum, hasCat[node]);
        }
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        m = sc.nextInt();
        hasCat = new boolean[n];
        for (int i = 0; i < n; i++){
            hasCat[i] = sc.nextInt() == 1;
        }
        tree = new List[n];
        for (int i = 0; i < n - 1; i++){
            int from = sc.nextInt() - 1;
            int to = sc.nextInt() - 1;
            if (tree[from] == null){
                tree[from] = new ArrayList<Integer>();
            }
            tree[from].add(to);
        }
        dfs(0, 0, false);
        System.out.println(result);
    }
}


// Author: Param Singh <paramsingh258@gmail.com>
// Kefa and Park: http://codeforces.com/contest/580/problem/C

import java.io.*;
import java.util.*;
public class Kefa {
    static class Node {
        List<Node> children;
        int num;
        boolean cat;
        int left, right;
        Node() {
            left = right = 0;
            children = new ArrayList<Node>();
            num = 0;
            cat = false;
        }

        void add_child(Node x) {
            num++;
            children.add(x);
        }

        void add_cat() {
            cat = true;
        }
    }

    public static int num_leafs(Node vertex, int consecutive, int m) {
        if (vertex.num == 0) {
            if (vertex.cat)
                consecutive++;
            if (m >= consecutive)
                return 1;
            else
                return 0;
        }
        else {
            int ans = 0;
            if (vertex.cat) {
                if (m == consecutive)
                    return 0;
                else {
                    for (Node x: vertex.children) {
                        ans += num_leafs(x, consecutive+1, m);
                    }
                    return ans;
                }
            }
            else {
                for (Node x: vertex.children) {
                    ans += num_leafs(x, 0, m);
                }
                return ans;
            }
        }
    }

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String s[] = in.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        int m = Integer.parseInt(s[1]);

        s = in.readLine().split(" ");
        Node tree[] = new Node[n];

        for (int i = 0; i < n; i++) {
            tree[i] = new Node();
            if (s[i].equals("1"))
                tree[i].add_cat();
        }

        int a, b;
        for (int i = 1; i < n; i++) {
            s = in.readLine().split(" ");
            a = Integer.parseInt(s[0]);
            b = Integer.parseInt(s[1]);
            tree[a-1].add_child(tree[b-1]);
        }
        System.out.println(num_leafs(tree[0], 0, m));
    }
}


// Author: Param Singh <paramsingh258@gmail.com>
// Kefa and Park: http://codeforces.com/contest/580/problem/C

import java.io.*;
import java.util.*;
public class Kefa {
    static class Node {
        List<Node> children;
        int id;
        int num;
        boolean cat;
        int left, right;
        Node() {
            left = right = 0;
            children = new ArrayList<Node>();
            num = 0;
            cat = false;
        }

        void add_child(Node x) {
            num++;
            children.add(x);
        }

        void add_cat() {
            cat = true;
        }
    }

    public static int num_leafs(Node vertex, Node prev, int consecutive, int m) {
        if (vertex.num == 1) {
            if (vertex.cat)
                consecutive++;
            if (m >= consecutive)
                return 1;
            else
                return 0;
        }
        else {
            int ans = 0;
            if (vertex.cat) {
                if (m == consecutive)
                    return 0;
                else {
                    for (Node x: vertex.children) {
                        if (prev != null && prev.id == x.id)
                            continue;
                        ans += num_leafs(x, vertex, consecutive+1, m);
                    }
                    return ans;
                }
            }
            else {
                for (Node x: vertex.children) {
                    if (prev != null && prev.id == x.id)
                        continue;
                    ans += num_leafs(x, vertex, 0, m);
                }
                return ans;
            }
        }
    }

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String s[] = in.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        int m = Integer.parseInt(s[1]);

        s = in.readLine().split(" ");
        Node tree[] = new Node[n];

        for (int i = 0; i < n; i++) {
            tree[i] = new Node();
            tree[i].id = i;
            if (s[i].equals("1"))
                tree[i].add_cat();
        }

        int a, b;
        for (int i = 1; i < n; i++) {
            s = in.readLine().split(" ");
            a = Integer.parseInt(s[0]);
            b = Integer.parseInt(s[1]);
            tree[a-1].add_child(tree[b-1]);
            tree[b-1].add_child(tree[a-1]);
        }
        System.out.println(num_leafs(tree[0], null, 0, m));
    }
}


#!/usr/bin/env python
n = int(raw_input())
m = (n+1) // 2
colors = set()
free = {}
paid = {}
for i in xrange(n):
	a,b = map(int,raw_input().split())
	colors.add(a)
	colors.add(b)
	free[a] = free.get(a,0) + 1
	if a != b:
		paid[b] = paid.get(b,0) + 1
mi = m+1
for color in colors:
	mm = max(0,m - free.get(color,0))
	if mm == 0:
		mi = 0
	elif color in paid and paid[color] >= mm:
		mi = min(mi,mm)
print mi if mi <= m else -1


import sys
n = int(sys.stdin.readline())
a = dict()
b = dict()
c = set()
m = (n+1)/2
for i in xrange(n):
	x, y = map(int, sys.stdin.readline().split())
	if x != y: a[y] = a.get(y, 0) + 1
	b[x] = b.get(x, 0) + 1
	c.add(x); c.add(y)
ans = n + 10
for k in c:
	if a.get(k, 0) + b.get(k, 0) >= m:
		ans = min(ans, (m - b.get(k, 0)))
ans = max(ans, 0)
if ans > n: ans = -1
print ans


import sys
n = int(sys.stdin.readline())
a = dict()
b = dict()
c = set()
m = (n+1)/2
for i in xrange(n):
	x, y = map(int, sys.stdin.readline().split())
	a[y] = a.get(y, 0) + 1
	b[x] = b.get(x, 0) + 1
	c.add(x); c.add(y)
ans = n + 10
for k in c:
	if a.get(k, 0) + b.get(k, 0) >= m:
		ans = min(ans, (m - b.get(k, 0)))
ans = max(ans, 0)
if ans > n: ans = -1
print ans


n=input()

from collections import defaultdict as D
C=D(lambda:[0,0])
for i in range(n):
    a,b=map(int,raw_input().split())
    C[a][0]+=1
    if a!=b:
        C[b][1]+=1
m=[-(-n/2)-a for k,(a,b) in C.items() if a+b >= -(-n/2)]
print max(min(m),0) if m else -1


n=input()

from collections import defaultdict as D
C=D(lambda:[0,0])
for i in range(n):
    a,b=map(int,raw_input().split())
    C[a][0]+=1
    C[b][1]+=1

print max(min(-(-n/2)-a for k,(a,b) in C.items() if a+b >= -(-n/2)),0)


n=input()

from collections import defaultdict as D
C=D(lambda:[0,0])
for i in range(n):
    a,b=map(int,raw_input().split())
    C[a][0]+=1
    C[b][1]+=1
m=[-(-n/2)-a for k,(a,b) in C.items() if a+b >= -(-n/2)]
print max(min(m),0) if m else -1


n=int(raw_input())
a=[]
for i in xrange(n):
    a.append(map(int,raw_input().split()))
dd,dd2={},{}
z=set()
for i,j in a:
    dd[i]=dd.get(i,0)+1
    if i!=j:
        dd2[j]=dd2.get(j,0)+1
    z.add(i)
    z.add(j)
x=(n+1)/2
mm=-1
for i in z:
#    print i,dd.get(i,0),dd2.get(i,0),x
    if dd.get(i,0)+dd2.get(i,0)>=x:
        tre=x-dd.get(i,0)
        if tre<0:
            tre=0
        if (mm==-1)or(mm>tre):
            mm=tre
print mm


import sets
a=input();
du={};
dl={};
keys=[]
for i in range(0,a):
	x,y=map(int,raw_input().split());
	try:
		du[x]=du[x]+1
	except:
		du[x]=1;
	if(x!=y):
		try:
			dl[y]=dl[y]+1
		except:
			dl[y]=1;
	keys.append(x)
	keys.append(y)

keys=set(keys)
s=a/2;
if(2*s!=a):
	s=s+1;
xx=0;
skey=0;
ans=-1;
#print du
#print dl
minn=1000000000
for key in keys:
	try:
		x=du[key]
	except:
		x=0
	try:
		y=dl[key]
	except:
		y=0
	try:
		if(x+y>=s):
			ans=s-du[key];
			if(ans<minn):
				minn=ans
	except:
		pass;
#print ans
print minn


a=input();
du={};
dl={};
for i in range(0,a):
	x,y=map(int,raw_input().split());
	try:
		du[x]=du[x]+1
	except:
		du[x]=1;
	if(x!=y):
		try:

			dl[y]=dl[y]+1
		except:
			dl[y]=1;
s=a/2;
if(2*s!=a):
	s=s+1;
xx=0;
skey=0;
ans=-1;
#print du
#print dl
for key in du.keys():
	try:
		if(du[key]+dl[key]>=s):
			ans=s-du[key];
			break;
	except:
		pass;
print ans


import sets
a=input();
du={};
dl={};
keys=[]
for i in range(0,a):
	x,y=map(int,raw_input().split());
	try:
		du[x]=du[x]+1
	except:
		du[x]=1;
	if(x!=y):
		try:
			dl[y]=dl[y]+1
		except:
			dl[y]=1;
	keys.append(x)
	keys.append(y)

keys=set(keys)
s=a/2;
if(2*s!=a):
	s=s+1;
xx=0;
skey=0;
ans=-1;
#print du
#print dl
minn=1000000000
for key in keys:
	try:
		x=du[key]
	except:
		x=0
	try:
		y=dl[key]
	except:
		y=0
	try:
		#		print x+y,s
		if(x+y>=s):
			ans=s-x;
			if(ans<0):
				ans=0;
			if(ans<minn):
				minn=ans
	except:
		pass;
#print ans
if(minn==1000000000):
	print "-1"
else:
	print minn


n = int(raw_input())
m = (n + 1) / 2
a = {}
b = {}
s = set()

for i in xrange(n):
	x, y = map(int, raw_input().split())
	s.add(x)
	s.add(y)
	a[x] = a.get(x, 0) + 1
	if x != y: b[y] = b.get(y, 0) + 1

r = n + 1
for i in s:
	if a.get(i, 0) + b.get(i, 0) >= m:
		r = min(r, m - a.get(i, 0))
r = max(r, 0)
if r > n: r = -1
print r


n=int(raw_input())
m=(n+1)/2
a={}
b={}
s=set()
for i in range(n):
	x,y=map(int,raw_input().split())
	s.add(x)
	s.add(y)
	a[x]=a.get(x,0)+1
	if x!=y:b[y]=b.get(y,0)+1
r=n+1
for i in s:
	if a.get(i, 0)+b.get(i, 0)>=m: r=min(r,m-a.get(i,0))
r=max(r,0)
if r>n:r=-1
print r


N = int(raw_input())
lim = (N+1)/2
ans = lim + 929
cntFore = {}
cntBack = {}
color = set()

for i in range(N):
	f,b = map(int,raw_input().split())
	color.add(f), color.add(b)
	cntFore[f] = cntFore.get(f,0) + 1
	if f==b: continue
	cntBack[b] = cntBack.get(b,0) + 1
for c in color:
	upwardsAmount = cntFore.get(c,0) + cntBack.get(c,0)
	if upwardsAmount >= lim:
		ans = min(ans, lim - cntFore.get(c,0))
if ans < 0:
	ans = 0
if ans > lim:
	ans = -1
print ans


def main():

    n = int(raw_input())

    Front = {}
    Back = {}
    Col = []

    for i in range(n):
        a, b = map(int, raw_input().split(' '))
        Col += [a, b]

        if (a in Front):
            Front[a] += 1
        else:
            Front[a] = 1

        if (a != b):
            if (b in Back):
                Back[b] += 1
            else:
                Back[b] = 1

    best = n + 1
    req = (n + 1)/2

    for i in Col:
        here = 0

        if (i in Front):
            here = Front[i]

        if (here < req):
            rest = req - here

            if (i in Back and rest <= Back[i]):
                best = min(best, rest)
        else:
            best = 0
            break

    if (best > n):
        print -1
    else:
        print best

if __name__ == '__main__':
    main()


import sys

N=int(sys.stdin.readline())
req=N/2
if N % 2 == 1:
    req+=1
fronts={}
backs={}
both={}

def update(c,map):
    map[c]=map.get(c,0)+1

for i in range(N):
    f,b=[long(x) for x in sys.stdin.readline().strip().split(" ")]
    update(f,fronts)
    update(b,backs)
    update(f,both)
    if f!=b:
        update(b,both)

fronts_sorted=sorted([(fronts[x],x) for x in fronts.keys()], reverse=True)
#print fronts_sorted[0]
if fronts_sorted[0][0]>=req:
    print "0"
else:
    both_sorted=sorted([(both[x],x) for x in both.keys()], reverse=True)
    #print both_sorted[0]
    if both_sorted[0][0]<req:
        print "-1"
    else:
        for count,color in fronts_sorted:
            #print "ad", count,color,req
            if both[color]>=req:
                todo=req-count
                print "%d"%(todo)
                break
        else:
            print "%d"%req


import sys

N=int(sys.stdin.readline())
req=N/2
if N % 2 == 1:
    req+=1
fronts={}
backs={}
both={}

def update(c,map):
    map[c]=map.get(c,0)+1

for i in range(N):
    f,b=[long(x) for x in sys.stdin.readline().strip().split(" ")]
    update(f,fronts)
    update(b,backs)
    update(f,both)
    if f!=b:
        update(b,both)

fronts_sorted=sorted([(fronts[x],x) for x in fronts.keys()], reverse=True)
#print fronts_sorted[0]
if fronts_sorted[0][0]>=req:
    print "0"
else:
    #print both
    both_sorted=sorted([(both[x],x) for x in both.keys()], reverse=True)
    #print both_sorted[0]
    if both_sorted[0][0]<req:
        print "-1"
    else:
        for count,color in fronts_sorted:
            #print "ad", count,color,req
            if both[color]>=req:
                todo=req-count
                print "%d"%(todo)
                break
        else:
            assert 0


try:
    while True:
        dc = {}
        n = input()
        for x in range(n):
            f,b = map(int, raw_input().split())
            if f not in dc: dc[f] = [0, 0]
            if b not in dc: dc[b] = [0, 0]
            if f==b:
                dc[f][0] += 1
            else:
                dc[f][0] += 1
                dc[b][0] += 1
                dc[b][1] += 1
        ans = 1<<29;
        tar = (n+1)/2
        for v in dc.itervalues():
            if v[0]<tar: continue
            t = tar - (v[0] - v[1])
            if t < 0: t = 0
            ans = min(ans, t)

        print ans if ans!= (1<<29) else -1

except Exception,e:
    pass


try:
    while True:
        dc = {}
        n = input()
        for x in range(n):
            f,b = map(int, raw_input().split())
            if f not in dc: dc[f] = [0, 0]
            if b not in dc: dc[b] = [0, 0]
            if f==b:
                dc[f][0] += 1
            else:
                dc[f][0] += 1
                dc[b][0] += 1
                dc[b][1] += 1
        ans = 1<<29;
        tar = (n+1)/2
        for k,v in dc.items():
            if v[0]<tar: continue
            t = tar - (v[0] - v[1])
            if t < 0: t = 0
            ans = min(ans, t)

        print ans

except Exception,e:
    pass


try:
    while True:
        dc = {}
        n = input()
        for x in range(n):
            f,b = map(int, raw_input().split())
            if f not in dc: dc[f] = [0, 0]
            if b not in dc: dc[b] = [0, 0]
            if f==b:
                dc[f][0] += 1
            else:
                dc[f][0] += 1
                dc[b][0] += 1
                dc[b][1] += 1
        ans = 1<<29;
        tar = (n+1)/2
        for k,v in dc.items():
            if v[0]<tar: continue
            t = tar - (v[0] - v[1])
            if t < 0: t = 0
            ans = min(ans, t)

        print ans if ans!= (1<<29) else -1

except Exception,e:
    pass


from sys import stdin
import fractions, time, math, random

def line():
    return stdin.readline().split()
def read_int():
    return int(line()[0])
def read_ints():
    return [int(x) for x in line()]
def memo(fn):
    table = {}
    def memoized(*args):
        if args not in table:
            table[args] = fn(*args)
        return table[args]
    return memoized

def increment(dct, val):
    if val not in dct:
        dct[val] = 0
    dct[val] += 1

front_colors = {}
back_colors = {}
same_colors = {}
colors = set()

n = read_int()
for _ in range(n):
    front, back = read_ints()
    if front == back:
        increment(same_colors, front)
    else:
        increment(front_colors, front)
        increment(back_colors, back)
    colors.add(front)
    colors.add(back)

best_color, best_needed = None, 10**6
half = float(n)/2
for color in colors:
    current = front_colors.get(color, 0) + same_colors.get(color, 0)
    backs = back_colors.get(color, 0)
    if (current + backs) < half: continue

    if current < half:
        needed = int(math.ceil(half)) - current
    else:
        needed = 0

    if needed < best_needed:
        best_color, best_needed = color, needed

if best_color:
    print best_needed
else:
    print -1


using System;
using System.Linq;
using System.Collections.Generic;
class Program{
	static void Main(){
		int n = int.Parse(Console.ReadLine());
		int m = (int)Math.Ceiling(n / 2.0);

		var Color = new HashSet<int>();
		var fColor = new List<int>();
		var bColor = new List<int>();
		var fColorCount = new List<int>();
		var bColorCount = new List<int>();

		for(int i = 0; i < n; i++){
			string[] line = Console.ReadLine().Split();
			int f = int.Parse(line[0]);
			int b = int.Parse(line[1]);

			Color.Add(f);
			Color.Add(b);

			//front
			int fi = fColor.IndexOf(f);
			if(fi != -1){
					fColorCount[fi]++;
			}else{
				fColor.Add(f);
				fColorCount.Add(1);
			}
			//back
			if(f == b) continue;

			int bi = bColor.IndexOf(b);
			if(bi != -1){
					bColorCount[bi]++;
			}else{
				bColor.Add(b);
				bColorCount.Add(1);
			}
		}

		int min = 1000000001;
		foreach(var color in Color){
			int fi = fColor.IndexOf(color);
			int bi = bColor.IndexOf(color);

			int fc = 0;
			int bc = 0;

			if(fi != -1)
				fc = fColorCount[fi];
			if(bi != -1)
				bc = bColorCount[bi];

			if( fc + bc >= m){
				if(m - fc < min){
					min = m - fc;
					if(min < 0) min = 0;
				}
			}
		}

		if(min == 1000000001)
			Console.WriteLine(-1);
		else
			Console.WriteLine(min);
	}
}


using System;
using System.Linq;
class Test {
    static int Main() {
        int n = int.Parse(Console.ReadLine());
        int d = n/2;
        if(n%2!=0){
            d++;
        }

        int [] x = new int[n];
        int [] y = new int[n];
        int i, j, k;

        for(i=0 ; i<n ; i++){
            string [] str = Console.ReadLine().Split();
                x[i] = int.Parse(str[0]);
                y[i] = int.Parse(str[1]);
                if(x[i]==y[i]){
                    y[i] = -1;
                }
        }

        int [] cp = new int [n];
        int [] cps = new int [n];
        int [] len = new int [n];
        Array.Copy(x, cp, x.Length);
        Array.Sort(cp);
        i = j = 0;
        k = 0;
        while(true){
            k = Array.LastIndexOf(cp, cp[j]);
            len[i] = k-Array.IndexOf(cp, cp[j])+1;
            cps[i]= cp[k];
            j = k+1;
            i++;
            if(j>x.Length-1){
                break;
            }
        }

        if(len.Max()>=d){
            Console.WriteLine(0);
            return 0;
        }

        i = 0;
        int ct = 0;
        while(true){
            if(i>len.Length-1){
                break;
            }
            k = Array.IndexOf(len, len.Max());
            for(j=0 ; j<n ; j++){
                if(cps[k]==y[j]){
                    ct++;
                }
                if(ct+len[k]>=d){
                    Console.WriteLine(d-len[k]);
                    return 0;
                }
            }
            ct = 0;
            len[k] = -1;
            i++;
        }

        Array.Copy(y, cp, y.Length);
        Array.Sort(cp);
        i = j = 0;
        k = 0;
        while(true){
            k = Array.LastIndexOf(cp, cp[j]);
            len[i] = k-Array.IndexOf(cp, cp[j])+1;
            j = k+1;
            i++;
            if(j>x.Length-1){
                break;
            }
        }
        if(len.Max()>=d){
            Console.WriteLine(d);
        }else{
            Console.WriteLine(-1);
        }
        return 0;
    }
}


using System;
using System.Linq;
class Test {
    static int Main(){
        int n = int.Parse(Console.ReadLine());
        int [] a = new int [n];
        int [] b = new int [n];
        int i;
        for( i=0 ; i<n ; i++){
            string [] tmp = Console.ReadLine().Split();
            a[i] = int.Parse(tmp[0]);
            b[i] = int.Parse(tmp[1]);
        }
        int [] t = new int [n];
        Array.Copy(a,t,a.Length);
        Array.Sort(t);
        int [] index = new int [n];
        int [] value = new int [n];
        value[0] = t[0];
        int len = 0;
        for(int k=0, j=1, p=0 ; j<=n ; j++){
             if(j==n){
                len = j-k;
                index[p] = len;
                break;
            }
            if(t[k]!=t[j]){
                len = j-k;
                k = j;
                index[p] = len;
                value[p+1] = t[j];
                p++;
            }
        }
        int d = n/2;
        if(n%2!=0){
            d++;
        }
        int count = 0;
        if(index.Max()>=d){
            Console.WriteLine(0);
            return 0;
        }else{
            int p = 0;
            while(true){
                if(p==index.Length){
                    Console.WriteLine(-1);
                    return 0;
                }
                for(i=0 ; i<n ; i++){
                    if(b[i]==value[p]){
                        if(b[i]!=a[i]){
                            count++;
                        }
                    }
                }
                if(count+index[p]>=d){
                    Console.WriteLine(count);
                    return 0;
                }else{
                    p++;
                    count = 0;
                }
            }
        }
    }
}