Digital collectible card games have become very popular recently. So Vova decided to try one of these.

Vova has n cards in his collection. Each of these cards is characterised by its power pi, magic number ci and level li. Vova wants to build a deck with total power not less than k, but magic numbers may not allow him to do so — Vova can't place two cards in a deck if the sum of the magic numbers written on these cards is a prime number. Also Vova cannot use a card if its level is greater than the level of Vova's character.

At the moment Vova's character's level is 1. Help Vova to determine the minimum level he needs to reach in order to build a deck with the required total power.

Input

The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100000).

Then n lines follow, each of these lines contains three numbers that represent the corresponding card: pi, ci and li (1 ≤ pi ≤ 1000, 1 ≤ ci ≤ 100000, 1 ≤ li ≤ n).

Output

If Vova won't be able to build a deck with required power, print  - 1. Otherwise print the minimum level Vova has to reach in order to build a deck.

Examples

Input

5 8
5 5 1
1 5 4
4 6 3
1 12 4
3 12 1


Output

4


Input

3 7
4 4 1
5 8 2
5 3 3


Output

2
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.StringTokenizer;

/*
 public class _808F {

 }

 */

public class _808F
{
	class Dinic_maxflow
	{
		class Node
		{
			int v;
			int flow;
			int cap;
			int rei;

			public Node(int v, int flow, int cap, int rei)
			{
				super();
				this.v = v;
				this.flow = flow;
				this.cap = cap;
				this.rei = rei;
			}
		}

		int n;
		List<ArrayList<Node>> g;
		int s;
		int t;

		int[] level;

		public Dinic_maxflow(int n, int s, int t)
		{
			this.n = n;
			this.g = new ArrayList<ArrayList<Node>>();
			for (int i = 0; i < n; i++)
				g.add(new ArrayList<Node>());
			this.level = new int[n];
			this.s = s;
			this.t = t;
		}

		void addEdge(int u, int v, int cap)
		{
			g.get(u).add(new Node(v, 0, cap, g.get(v).size()));
			g.get(v).add(new Node(u, 0, 0, g.get(u).size() - 1));
		}

		boolean bfs()
		{
			Deque<Integer> q = new ArrayDeque<Integer>();

			for (int i = 0; i < n; i++)
			{
				level[i] = -1;
			}

			q.add(s);
			level[s] = 0;

			while (!q.isEmpty())
			{
				int u = q.poll();

				ArrayList<Node> ul = g.get(u);
				int pl = level[u];

				for (int i = 0; i < ul.size(); i++)
				{
					Node nv = ul.get(i);

					int v = nv.v;
					int fa = nv.cap - nv.flow;

					if (level[v] == -1 && fa > 0)
					{
						q.add(v);
						level[v] = pl + 1;
					}
				}
			}

			return level[t] != -1;
		}

		int sendFlow(int u, int[] start, int maxFlow)
		{
			if (u == t)
			{
				return maxFlow;
			}

			ArrayList<Node> ul = g.get(u);
			for (; start[u] < ul.size(); start[u]++)
			{
				int i = start[u];

				Node vn = ul.get(i);
				int v = vn.v;
				int f = vn.cap - vn.flow;

				if (level[v] == level[u] + 1 && f > 0)
				{
					int flow = sendFlow(v, start, Math.min(f, maxFlow));

					if (flow == 0)
					{
						continue;
					}

					vn.flow += flow;
					g.get(v).get(vn.rei).flow -= flow;

					return flow;
				}
			}

			return 0;
		}

		int maxFlow()
		{
			int maxFlow = 0;

			while (bfs())
			{
				int[] start = new int[n];

				int rf;
				while ((rf = sendFlow(s, start, Integer.MAX_VALUE)) > 0)
				{
					maxFlow += rf;
				}
			}

			return maxFlow;
		}

	}

	public void solve() throws FileNotFoundException
	{
		InputStream inputStream = System.in;

		InputHelper inputHelper = new InputHelper(inputStream);

		PrintStream out = System.out;

		// actual solution
		int n = inputHelper.readInteger();
		int k = inputHelper.readInteger();

		int[] p = new int[n];
		int[] c = new int[n];
		int[] l = new int[n];

		for (int i = 0; i < n; i++)
		{
			p[i] = inputHelper.readInteger();
			c[i] = inputHelper.readInteger();
			l[i] = inputHelper.readInteger();
		}

		boolean[][] isp = new boolean[n][n];

		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				if (c[i] % 2 == 1 && c[j] % 2 == 0 && isprime(c[i] + c[j]))
				{
					isp[i][j] = true;
				}
			}
		}

		int lo = 1;
		int hi = n;

		if (!isPoss(p, c, l, n, isp, n, k))
		{
			System.out.println("-1");
			return;
		}

		int ans = n;
		while (lo <= hi)
		{
			int mid = (hi + lo) / 2;

			if (isPoss(p, c, l, mid, isp, n, k))
			{
				ans = Math.min(ans, mid);
				hi = mid - 1;
			}
			else
			{
				lo = mid + 1;
			}
		}

		System.out.println(ans);
		// end here
	}

	private boolean isPoss(int[] p, int[] c, int[] l, int mid, boolean[][] isp,
			int n, int k)
	{
		ArrayList<Integer> ml = new ArrayList<Integer>();

		int sum = 0;
		int max1pow = -1;
		int max1ind = -1;
		for (int i = 0; i < n; i++)
		{
			if (l[i] <= mid)
			{
				if (c[i] == 1)
				{
					if (p[i] > max1pow)
					{
						max1pow = p[i];
						max1ind = i;
					}
				}
				else
				{
					ml.add(i);
					sum += p[i];
				}
			}
		}

		if (max1ind != -1)
		{
			ml.add(max1ind);
			sum += max1pow;
		}

		int mls = ml.size();

		Dinic_maxflow maxflow = new Dinic_maxflow(mls + 2, 0, mls + 1);

		for (int i = 0; i < ml.size(); i++)
		{
			if (c[ml.get(i)] % 2 == 1)
			{
				maxflow.addEdge(0, i + 1, p[ml.get(i)]);
			}
			else
			{
				maxflow.addEdge(i + 1, mls + 1, p[ml.get(i)]);
			}
		}

		for (int i = 0; i < mls; i++)
		{
			for (int j = 0; j < mls; j++)
			{
				if (i != j && c[ml.get(i)] % 2 == 1 && c[ml.get(j)] % 2 == 0
						&& isp[ml.get(i)][ml.get(j)])
				{
					maxflow.addEdge(i + 1, j + 1, (int) 1e9);
				}
			}
		}

		if (sum - maxflow.maxFlow() >= k)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	boolean isprime(int num)
	{
		if (num == 2)
		{
			return true;
		}

		for (int i = 2; i * i <= num; i++)
		{
			if (num % i == 0)
			{
				return false;
			}
		}

		return true;
	}

	public static void main(String[] args) throws FileNotFoundException
	{
		(new _808F()).solve();
	}

	class InputHelper
	{
		StringTokenizer tokenizer = null;
		private BufferedReader bufferedReader;

		public InputHelper(InputStream inputStream)
		{
			InputStreamReader inputStreamReader = new InputStreamReader(
					inputStream);
			bufferedReader = new BufferedReader(inputStreamReader, 16384);
		}

		public String read()
		{
			while (tokenizer == null || !tokenizer.hasMoreTokens())
			{
				try
				{
					String line = bufferedReader.readLine();
					if (line == null)
					{
						return null;
					}
					tokenizer = new StringTokenizer(line);
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}

			return tokenizer.nextToken();
		}

		public Integer readInteger()
		{
			return Integer.parseInt(read());
		}

		public Long readLong()
		{
			return Long.parseLong(read());
		}
	}
}