Ivan has $n$ songs on his phone. The size of the $i$-th song is $a_i$ bytes. Ivan also has a flash drive which can hold at most $m$ bytes in total. Initially, his flash drive is empty.

Ivan wants to copy all $n$ songs to the flash drive. He can compress the songs. If he compresses the $i$-th song, the size of the $i$-th song reduces from $a_i$ to $b_i$ bytes ($b_i < a_i$).

Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $m$. He can compress any subset of the songs (not necessarily contiguous).

Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $m$).

If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress.


-----Input-----

The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 10^5, 1 \le m \le 10^9$) — the number of the songs on Ivan's phone and the capacity of Ivan's flash drive.

The next $n$ lines contain two integers each: the $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^9$, $a_i > b_i$) — the initial size of the $i$-th song and the size of the $i$-th song after compression.


-----Output-----

If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress.


-----Examples-----
Input
4 21
10 8
7 4
3 1
5 4

Output
2

Input
4 16
10 8
7 4
3 1
5 4

Output
-1



-----Note-----

In the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $8 + 7 + 1 + 5 = 21 \le 21$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $8 + 4 + 3 + 5 = 20 \le 21$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $10 + 4 + 3 + 5 = 22 > 21$).

In the second example even if Ivan compresses all the songs the sum of sizes will be equal $8 + 4 + 1 + 4 = 17 > 16$.

Source: codeforces | Difficulty: EASY
Tags: Sorting


Solutions:
(n, m) = map(int, input().split())
A = []
B = []
diff = []
for i in range(n):
	(a, b) = map(int, input().split())
	A.append(a)
	B.append(b)
	diff.append(a - b)
diff.sort(reverse=True)
if sum(B) > m:
	print(-1)
else:
	x = sum(A)
	i = 0
	while x > m:
		x -= diff[i]
		i += 1
	print(i)


import math
import itertools
from collections import defaultdict
getInputList = lambda : list(input().split())
getInputIntList = lambda : list(map(int, input().split()))
(n, m) = getInputIntList()
s = 0
arr = []
for i in range(n):
	(a, b) = getInputIntList()
	s += a
	arr.append(a - b)
step = 0
arr.sort()
while s > m and len(arr) > 0:
	s -= arr.pop()
	step += 1
if s <= m:
	print(step)
else:
	print(-1)


(n, m) = map(int, input().strip().split(' '))
c = []
s = 0
s1 = 0
for i in range(n):
	(a, b) = map(int, input().strip().split(' '))
	c.append(a - b)
	s += b
	s1 += a
if s > m:
	print('-1')
elif s1 <= m:
	print(0)
else:
	c.sort()
	count = 0
	for i in range(n):
		if s + c[i] <= m:
			s += c[i]
		else:
			break
	print(n - i)


q = list(map(int, input().strip().split()))
b = []
c = []
s = 0
x = 0
j = 0
for i in range(q[0]):
	b.append(list(map(int, input().strip().split())))
	c.append(b[i][0] - b[i][1])
	s = s + b[i][0]
c.sort(reverse=True)
while s > q[1] and x < q[0]:
	s = s - c[j]
	j = j + 1
	x = x + 1
if x == q[0] and s > q[1]:
	print(-1)
else:
	print(x)


(n, m) = map(int, input().split())
diff = []
total = 0
for i in range(n):
	(a, b) = map(int, input().split())
	diff.append(a - b)
	total += b
if total > m:
	print(-1)
else:
	diff.sort()
	i = 0
	n_compressed = n
	while i < n and total + diff[i] <= m:
		total += diff[i]
		i += 1
		n_compressed -= 1
	print(n_compressed)


import sys
import string
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from itertools import combinations, permutations
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())
(n, k) = r()
arr = [rr() for _ in range(n)]
arr.sort(key=lambda x: x[0] - x[1])
x = sum((j for (i, j) in arr))
if x > k:
	exit(print(-1))
ans = n
for (i, j) in arr:
	if x + (i - j) <= k:
		ans -= 1
		x += i - j
print(ans)


(n, m) = map(int, input().split())
H = []
k = 0
kl = 0
for i in range(n):
	(a, b) = map(int, input().split())
	H.append(a - b)
	k += a
	kl += b
if kl > m:
	print(-1)
	exit()
H.sort()
H.reverse()
p = 0
while True:
	if k <= m:
		print(p)
		exit()
	k -= H[p]
	p += 1


(n, m) = map(int, input().split())
la = []
cur = 0
for i in range(n):
	(a, b) = map(int, input().split())
	la.append([a - b, a, b])
	cur += a
s = 0
la.sort(reverse=True)
for (c, a, b) in la:
	if cur <= m:
		break
	else:
		cur -= a - b
		s += 1
if cur > m:
	print(-1)
	exit()
print(s)


(n, m) = map(int, input().split())
d = []
s = 0
for i in range(n):
	(a, b) = map(int, input().split())
	s += a
	d += [a - b]
d.sort()
c = 0
while d != [] and s > m:
	s -= d.pop()
	c += 1
if d == [] and s > m:
	print(-1)
else:
	print(c)


(n, m) = map(int, input().split())
lis = []
t = c = 0
for i in range(n):
	(a, b) = map(int, input().split())
	t += a
	c += b
	lis.append([a - b, a, b])
lis.sort(reverse=True)
ans = i = 0
if c > m:
	print('-1')
	exit()
while t > m and i < n:
	t -= lis[i][0]
	i += 1
	ans += 1
if t > m:
	print(-1)
else:
	print(ans)


(n, m) = map(int, input().split())
sum_a = 0
sum_b = 0
arr = []
for i in range(n):
	(a, b) = map(int, input().split())
	sum_a += a
	sum_b += b
	arr.append(a - b)
if sum_a <= m:
	print(0)
elif sum_b > m:
	print(-1)
else:
	arr.sort(reverse=True)
	ans = 0
	i = 0
	while sum_a > m:
		ans += 1
		sum_a -= arr[i]
		i += 1
	print(ans)


import sys
(n, m) = list(map(int, input().split()))
a = []
b = []
diff = []
for x in range(n):
	(a1, b1) = list(map(int, input().split()))
	a.append(a1)
	b.append(b1)
	diff.append(a1 - b1)
sum_song = sum(a)
diff = sorted(diff, reverse=True)
if sum_song > m:
	for x in range(len(diff)):
		sum_song -= diff[x]
		if sum_song <= m:
			print(x + 1)
			sys.exit()
	if sum_song > m:
		print(-1)
		sys.exit()
else:
	print(0)


import sys
from math import *

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())
(n, m) = mints()
a = []
s = 0
for i in range(n):
	(x, y) = mints()
	s += x
	a.append(x - y)
a.sort(reverse=True)
r = 0
for i in a:
	if s <= m:
		break
	s -= i
	r += 1
if s <= m:
	print(r)
else:
	print(-1)


(n, m) = map(int, input().split())
a = []
s1 = 0
for i in range(n):
	tup = tuple(map(int, input().split()))
	a.append(tup[0] - tup[1])
	s1 = s1 + tup[0]
a.sort(reverse=True)
i = 0
while s1 > m and i != n:
	s1 -= a[i]
	i += 1
if s1 > m:
	print('-1')
else:
	print(i)


from sys import stdin
(n, m) = map(int, stdin.readline().split())
a = [0] * n
b = [0] * n
for i in range(n):
	(a[i], b[i]) = map(int, stdin.readline().split())
	b[i] = a[i] - b[i]
b.sort(reverse=True)
sum_a = sum(a)
if sum_a <= m:
	ans = 0
else:
	ans = -1
	for i in range(n):
		sum_a -= b[i]
		if sum_a <= m:
			ans = i + 1
			break
print(ans)


import sys
input = sys.stdin.readline
ins = lambda : input().rstrip()
ini = lambda : int(input().rstrip())
inm = lambda : map(int, input().rstrip().split())
inl = lambda : list(map(int, input().split()))
out = lambda x: print('\n'.join(map(str, x)))
(n, m) = inm()
a = 0
b = []
for _ in range(n):
	(i, j) = inm()
	a += i
	b.append(i - j)
if a <= m:
	print(0)
elif a - m > sum(b):
	print(-1)
else:
	b = sorted(b, reverse=True)
	count = a - m
	ans = 0
	for i in b:
		count -= i
		ans += 1
		if count <= 0:
			break
	print(ans)


(n, m) = map(int, input().split())
a = []
b = []
diff = []
for i in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	b.append(y)
	diff.append(x - y)
diff.sort(reverse=True)
ans = 0
if sum(b) > m:
	print(-1)
else:
	sum_a = sum(a)
	while sum_a > m:
		sum_a -= diff[ans]
		ans += 1
	print(ans)


(n, m) = map(int, input().split(' '))
l = []
s = 0
s1 = 0
for i in range(0, n):
	(a, b) = map(int, input().split(' '))
	l.append(a - b)
	s += b
	s1 += a
if s > m:
	print(-1)
elif s == m:
	print(n)
else:
	count = 0
	j = 0
	l.sort(reverse=True)
	while s1 > m:
		s1 = s1 - l[j]
		j = j + 1
		count += 1
	print(count)


input_list = list(map(int, input().split()))
n = input_list[0]
m = input_list[1]
tup_list = list()
orig_total = 0
comp_total = 0
for i in range(n):
	a_b = list(map(int, input().split()))
	orig_size = a_b[0]
	comp_size = a_b[1]
	diff_size = a_b[0] - a_b[1]
	orig_total = orig_total + orig_size
	comp_total = comp_total + comp_size
	tup_list.append((orig_size, comp_size, diff_size))
if comp_total > m:
	print('-1')
elif orig_total <= m:
	print('0')
else:
	sorted_tup_list = sorted(tup_list, key=lambda tup: tup[2], reverse=True)
	cur_size = orig_total
	count = 0
	for elem in sorted_tup_list:
		diff = elem[2]
		cur_size = cur_size - diff
		count = count + 1
		if cur_size <= m:
			print(count)
			break


import math as ma
from sys import exit
from decimal import Decimal as dec

def li():
	return list(map(int, input().split()))

def num():
	return map(int, input().split())

def nu():
	return int(input())
(n, mm) = num()
a = []
lp = 0
lk = 0
for i in range(n):
	(x, y) = num()
	lp += x
	lk += y
	a.append(x - y)
if lp <= mm:
	print(0)
elif lk > mm:
	print(-1)
else:
	a.sort()
	s = 0
	c = 0
	a = a[::-1]
	for i in range(n):
		s += a[i]
		c += 1
		if lp - s <= mm:
			break
	print(c)


(n, m) = map(int, input().split())
l = []
d = []
a = 0
b = 0
c = 0
for i in range(n):
	l.append(list(map(int, input().split())))
	a += l[i][0]
	b += l[i][1]
	d.append(l[i][0] - l[i][1])
if a <= m:
	print('0')
elif b > m:
	print('-1')
else:
	d.sort(reverse=True)
	for j in range(n):
		a -= d[j]
		c += 1
		if a <= m:
			break
	print(c)


import sys
(n, m) = map(int, input().split())
a = []
b = []
sum2 = 0
sum1 = 0
for i in range(n):
	(x, y) = map(int, input().split())
	a += [x - y]
	sum2 += y
	sum1 += x
if sum2 > m:
	print(-1)
	sys.exit()
a.sort(reverse=True)
ans = 0
if sum1 <= m:
	print(0)
	sys.exit()
for i in range(n):
	sum1 -= a[i]
	ans += 1
	if sum1 <= m:
		break
print(ans)


(n, max) = map(int, input().split())
menor = 0
total = 0
tirar = 0
diferencas = []
for i in range(n):
	(a, b) = map(int, input().split())
	diferencas.append(a - b)
	total += a
	menor += b
diferencas.sort(reverse=True)
if menor > max:
	print(-1)
else:
	for i in diferencas:
		if total <= max:
			break
		total -= i
		tirar += 1
	print(tirar)


import math
import os
import random
import re
import sys
a = input().split()
n = int(a[0])
m = int(a[1])
s1 = 0
s2 = 0
d = []
for i in range(n):
	b = input().split()
	s1 = s1 + int(b[0])
	s2 = s2 + int(b[1])
	d.append(int(b[0]) - int(b[1]))
if s1 <= m:
	print('0')
elif s2 > m:
	print('-1')
elif s2 == m:
	print(n)
else:
	d.sort(reverse=True)
	i = 0
	c = 0
	while 1:
		s1 = s1 - d[i]
		c = c + 1
		i = i + 1
		if s1 <= m:
			print(c)
			break


(n, m) = map(int, input().split())
songs = []
for _ in range(n):
	(a, b) = map(int, input().split())
	songs.append((a, b))
initial_sum = sum([item[0] for item in songs])
if sum([item[1] for item in songs]) > m:
	print('-1')
elif initial_sum <= m:
	print('0')
else:
	compressed_sum = 0
	temp_sum = 0
	songs = sorted(songs, key=lambda x: x[0] - x[1], reverse=True)
	for i in range(len(songs)):
		compressed_sum += songs[i][1]
		temp_sum += songs[i][0]
		if compressed_sum + (initial_sum - temp_sum) <= m:
			print(i + 1)
			break


f = lambda : list(map(int, input().split()))
(n, m) = f()
k = 0
dif = []
for _ in [0] * n:
	(a, b) = f()
	k += a
	dif.append(a - b)
dif.sort()
i = 0
while k > m and i < n:
	i += 1
	k -= dif[-i]
print((i, -1)[k > m])


(n, m) = map(int, input().split(' '))
a = []
d = []
for i in range(n):
	(x, y) = map(int, input().split(' '))
	a.append(x)
	d.append(x - y)
d = sorted(d, reverse=True)
s = sum(a)
ans = 0
for i in range(n):
	if s <= m:
		break
	else:
		s = s - d[i]
		ans += 1
if s <= m:
	print(ans)
else:
	print(-1)


(n, m) = map(int, input().split())
a = [0] * n
b = [0] * n
c = [0] * n
for i in range(n):
	(a[i], b[i]) = map(int, input().split())
	c[i] = a[i] - b[i]
if sum(b) > m:
	print(-1)
elif sum(a) <= m:
	print(0)
else:
	diff = sum(a) - m
	c = sorted(c)
	i = 0
	while diff > 0:
		diff -= c[~i]
		i += 1
	print(i)


(n, m) = map(int, input().split())
d = []
s1 = s2 = c = 0
for i in range(n):
	(x, y) = map(int, input().split())
	s1 += x
	s2 += y
	if x - y > 0:
		d.append(x - y)
if s2 > m:
	print(-1)
	exit()
if s1 <= m:
	print(0)
	exit()
d.sort(reverse=True)
r = s1 - m
for v in d:
	r -= v
	c += 1
	if r <= 0:
		break
print(c)


(n, m) = map(int, input().split())
s = 0
a = []
for i in range(n):
	(x, y) = map(int, input().split())
	s += x
	a.append(x - y)
a.sort(reverse=True)
i = 0
while i < n and s > m:
	s -= a[i]
	i += 1
if s <= m:
	print(i)
else:
	print(-1)


(n, m) = map(int, input().split())
a = []
b = []
d = []
for i in range(n):
	(t1, t2) = map(int, input().split())
	a.append(t1)
	b.append(t2)
	d.append(t1 - t2)
suma = sum(a)
sumb = sum(b)
d = sorted(d, reverse=True)
if suma <= m:
	print(0)
	exit()
elif sumb > m:
	print(-1)
	exit()
else:
	t = suma - m
	s = 0
	for i in range(n):
		s += d[i]
		if s >= t:
			print(i + 1)
			exit()


(p, m) = map(int, input().split())
songs = []
for i in range(p):
	(a, b) = map(int, input().split())
	songs.append([a - b, a, b])
songs = sorted(songs)
summ = 0
for el in songs:
	summ += el[1]
idx = p - 1
ans = 0
while summ > m:
	if idx < 0:
		ans = -1
		break
	summ -= songs[idx][0]
	ans += 1
	idx -= 1
print(ans)


(n, m) = list(map(int, input().split()))
s = []
k = 0
for i in range(n):
	(a, b) = list(map(int, input().split()))
	m -= a
	s.append(a - b)
s.sort(reverse=True)
while m < 0 and k < n:
	m += s[k]
	k += 1
if m < 0:
	print(-1)
else:
	print(k)


(n, m) = map(int, input().split())
reduce = []
s = 0
ans = 0
for i in range(n):
	(a, b) = map(int, input().split())
	s += a
	reduce.append(a - b)
reduce.sort(reverse=True)
i = 0
while s > m:
	ans += 1
	s -= reduce[i]
	i += 1
	if i >= n:
		break
if s > m:
	print(-1)
else:
	print(ans)


(n, m) = map(int, input().split())
x = []
y = 0
for i in range(n):
	(j, k) = map(int, input().split())
	y += j
	x.append(j - k)
x.sort(reverse=True)
ans = 0
for i in range(n):
	if y <= m:
		print(ans)
		exit(0)
	y -= x[i]
	ans += 1
if y <= m:
	print(ans)
else:
	print(-1)


(i_pios, miejsce) = map(int, input().split())
(a, b) = ([0 for _ in range(i_pios)], [0 for _ in range(i_pios)])
suma = 0
roznica = [0 for _ in range(i_pios)]
for x in range(i_pios):
	(a[x], b[x]) = map(int, input().split())
	suma += a[x]
	roznica[x] = a[x] - b[x]
wynik = 0
for x in sorted(roznica)[::-1]:
	if suma <= miejsce:
		break
	suma -= x
	wynik += 1
if suma <= miejsce:
	print(wynik)
else:
	print(-1)


(n, m) = map(int, input().split())
x = []
y = []
z = []
for i in range(n):
	(x1, y1) = [int(i) for i in input().split()]
	x.append(x1)
	y.append(y1)
	z.append(y1 - x1)
z = sorted(z)
if sum(y) > m:
	print(-1)
else:
	dai = sum(x)
	ans = 0
	i = 0
	while dai > m:
		ans += 1
		dai += z[i]
		i += 1
	print(i)


(n, m) = map(int, input().split())
ori = 0
red = 0
dif = []
for i in range(n):
	(x, y) = map(int, input().split())
	ori += x
	red += y
	dif.append(x - y)
if ori <= m:
	print(0)
elif red > m:
	print(-1)
else:
	tot = ori - m
	dif.sort(reverse=True)
	for i in range(n):
		tot -= dif[i]
		if tot <= 0:
			print(i + 1)
			break


(n, m) = [int(x) for x in input().split()]
al = []
c = []
bl = []
ans = 0
for i in range(n):
	(a, b) = [int(x) for x in input().split()]
	al.append(a)
	bl.append(b)
	c.append(a - b)
if sum(al) <= m:
	print('0')
elif sum(bl) > m:
	print('-1')
else:
	c.sort()
	d = sum(al) - m
	while d > 0:
		d -= c.pop()
		ans += 1
	print(ans)


import sys
(n, m) = map(int, input().split())
a = []
s = 0
for i in range(n):
	(x, y) = map(int, input().split())
	a.append(y - x)
	s += x
if s + sum(a) > m:
	print(-1)
	sys.exit(0)
a.sort()
i = 0
while s > m:
	s += a[i]
	i += 1
print(i)


def ind(k, i):
	if i == 0:
		return 0
	else:
		l = 0
		r = i - 1
		while True:
			if l == r:
				if D[l] > k:
					return l - 1
				else:
					return l
			m = (r + l) // 2
			if k == D[m]:
				return m
			if k > D[m]:
				r = m - 1
			else:
				l = m + 1
(n, m) = map(int, input().split())
D = [0] * n
s = 0
z = 0
ans = 0
for i in range(n):
	(a, b) = map(int, input().split())
	k = a - b
	D[i] = k
	s += a
	z += b
D.sort(reverse=True)
if z > m:
	print(-1)
else:
	for i in range(n):
		if s <= m:
			break
		s -= D[i]
		ans += 1
	print(ans)


(songs, maxSize) = map(int, input().split())
baseSize = 0
diff = []
for i in range(songs):
	(com, unCom) = map(int, input().split())
	baseSize += com
	diff.append(com - unCom)
if baseSize <= maxSize:
	print(0)
else:
	diff.sort(reverse=True)
	found = False
	for i in range(songs):
		baseSize -= diff[i]
		if baseSize <= maxSize:
			print(i + 1)
			found = True
			break
	if not found:
		print(-1)


import heapq
(n, m) = map(int, input().split())
s = []
d = []
for i in range(n):
	(a, b) = map(int, input().split())
	s.append(a)
	d.append(a - b)
d.sort(reverse=True)
c = sum(s)
if c <= m:
	print(0)
else:
	i = 0
	sum = 0
	while i < n and sum < c - m:
		sum += d[i]
		i += 1
	if i >= n and sum < c - m:
		print(-1)
	else:
		print(i)


(cnt, memory) = map(int, input().split())
deltas = [0] * cnt
musics = []
curr_memory = 0
for ind in range(cnt):
	(full, small) = map(int, input().split())
	musics.append([full, small])
	curr_memory += full
	deltas[ind] = full - small
deltas.sort()
count_changes = 0
steps = 0
while curr_memory > memory and steps < cnt:
	curr_memory -= deltas[cnt - steps - 1]
	steps += 1
if curr_memory <= memory:
	print(steps)
else:
	print(-1)


import operator
(n, m) = map(int, input().split())
a1 = []
a2 = []
for i in range(0, n):
	(a, b) = map(int, input().split())
	a1.append(a)
	a2.append(b)
d = {}
need = sum(a1)
diff = need - m
c = 0
for i in range(0, n):
	d[i] = a1[i] - a2[i]
sorted_d = sorted(d.items(), key=operator.itemgetter(1), reverse=True)
for i in sorted_d:
	if diff > 0:
		c = c + 1
		diff = diff - i[1]
	else:
		break
if diff > 0:
	print(-1)
else:
	print(c)


(n, m) = map(int, input().split(' '))
arr = [0] * n
tot = 0
for i in range(n):
	(a, b) = map(int, input().split(' '))
	arr[i] = (a, b)
	tot += arr[i][0]
arr = sorted(arr, key=lambda x: x[0] - x[1], reverse=True)
ind = 0
good = True
while tot > m:
	if ind == n:
		good = False
		break
	tot -= arr[ind][0]
	tot += arr[ind][1]
	ind += 1
if good:
	print(ind)
else:
	print(-1)


(n, m) = map(int, input().split())
s = -m
l = []
for _ in range(n):
	(a, b) = map(int, input().split())
	l.append(a - b)
	s += a
wyn = 0
for x in reversed(sorted(l)):
	if s > 0:
		s -= x
	else:
		break
	wyn += 1
print(wyn if s <= 0 else -1)


(n, phone) = map(int, input().split())
lst = []
sm = 0
for i in range(n):
	(fact, base) = map(int, input().split())
	sm += base
	lst.append((fact, base, fact - base))
if sm > phone:
	print(-1)
else:
	lst.sort(key=lambda elem: elem[2])
	i = 0
	while i < len(lst) and sm + lst[i][2] <= phone:
		sm += lst[i][2]
		i += 1
	print(n - i)


from heapq import *
(n, m) = map(int, input().split())
a = [0 for i in range(n)]
h = []
for i in range(n):
	(a[i], b) = map(int, input().split())
	heappush(h, -a[i] + b)
d = sum(a) - m
i = 0
while h and d > 0:
	i += 1
	d += heappop(h)
if d <= 0:
	print(i)
else:
	print(-1)


R = lambda : map(int, input().split())
(n, m) = R()
s = 0
a = []
for _ in [0] * n:
	(x, y) = R()
	s += x
	a += [x - y]
a.sort()
i = 0
while s > m and i < n:
	i += 1
	s -= a[-i]
print((i, -1)[s > m])


from sys import stdin

def main():
	(n, m) = map(int, input().split())
	l = []
	for s in stdin.read().splitlines():
		(a, b) = map(int, s.split())
		m -= b
		l.append(a - b)
	if m < 0:
		print(-1)
	else:
		l.sort()
		for (i, a) in enumerate(l):
			m -= a
			if m < 0:
				print(n - i)
				break
		else:
			print(0)
main()


(n, m) = [int(i) for i in input().split()]
arr = []
brr = []
crr = []
c = 0
d = 0
e = 0
for i in range(n):
	(a, b) = [int(i) for i in input().split()]
	arr.append(a)
	brr.append(a - b)
brr.sort()
for j in range(n):
	c = c + arr[j]
while d == 0:
	if e > n - 1:
		d = 1
	elif c > m:
		c = c - brr[n - e - 1]
		e = e + 1
	else:
		d = 1
if c > m:
	print(-1)
else:
	print(e)


R = lambda : map(int, input().split())
(n, m) = R()
s = 0
a = []
for _ in [0] * n:
	(x, y) = R()
	s += x
	a.append(x - y)
a.sort()
i = 0
while s > m:
	i += 1
	if i > n:
		break
	s -= a[-i]
print((i, -1)[s > m])


(n, k) = map(int, input().split())
origin = []
com = []
diff = []
for i in range(n):
	(a, b) = map(int, input().split())
	origin.append(a)
	diff.append(a - b)
sum = sum(origin)
diff.sort()
count = 0
if sum > k:
	for i in range(n - 1, -1, -1):
		sum = sum - diff[i]
		count += 1
		if sum <= k:
			break
if sum <= k:
	print(count)
else:
	print(-1)


(n, m) = input().split()
(n, m) = (int(n), int(m))
diff = []
s = 0
for i in range(1, n + 1):
	(a, b) = input().split()
	(a, b) = (int(a), int(b))
	c = b - a
	diff.append(c)
	s = s + a
diff.sort()
cp = 0
while cp < n and m < s:
	s = s + diff[cp]
	cp = cp + 1
if m >= s:
	print(cp)
else:
	print(-1)


(n, capacity) = map(int, input().split())
r1 = 0
l1 = []
l2 = []
for i in range(n):
	(a, b) = map(int, input().split())
	l1.append(a)
	l2.append(a - b)
r1 = sum(l1)
l2.sort(reverse=True)
if r1 - sum(l2) > capacity:
	print('-1')
	exit()
i = 0
while r1 > capacity:
	r1 -= l2[i]
	i += 1
print(i)


def main():
	(n, m) = map(int, input().split())
	diff = []
	_sum = 0
	for i in range(n):
		(a, b) = map(int, input().split())
		_sum += a
		diff.append(a - b)
	diff.sort(reverse=True)
	i = 0
	while _sum > m and i < n:
		_sum -= diff[i]
		i += 1
	print(i if _sum <= m else -1)
main()


b = list(map(int, input().split()))
n = b[0]
m = b[1]
o = []
c = []
l = []
for i in range(0, n):
	a = list(map(int, input().split()))
	o.append(a[0])
	c.append(a[1])
	l.append(a[0] - a[1])
l.sort()
z = len(l)
if sum(c) > m:
	print(-1)
else:
	total = sum(o)
	if total <= m:
		print('0')
	elif total > m:
		while total > m:
			total = total - l[z - 1]
			z = z - 1
		print(n - z)


list_1 = []
list_2 = []
list_3 = []
n_m = list(map(int, input().split()))
num = n_m[0]
memory = n_m[1]
for ex in range(num):
	a_b = list(map(int, input().split()))
	a = a_b[0]
	b = a_b[1]
	list_1.append(a)
	list_2.append(b)
if sum(list_1) <= memory:
	print('0')
n = 0
if sum(list_1) > memory:
	for i in list_1:
		razn = i - list_2[n]
		list_3.append(razn)
		n += 1
	list_3.sort()
	list_3.reverse()
	dlina = len(list_3)
	z = 0
	sch = 0
	summa = sum(list_1)
	try:
		while memory < summa:
			summa -= list_3[z]
			z += 1
			sch += 1
		if sch == dlina and memory < summa:
			print('-1')
		else:
			print(sch)
	except IndexError:
		print('-1')


(n, s) = map(int, input().split())
(l, lc, d) = ([], [], [])
for i in range(n):
	(a, b) = map(int, input().split())
	l.append(a)
	lc.append(b)
	d.append(a - b)
if sum(l) <= s:
	print('0')
elif sum(lc) > s:
	print('-1')
else:
	d.sort()
	d.reverse()
	i = 0
	sn = sum(l)
	while sn > s:
		sn -= d[i]
		i += 1
	print(i)


(n, m) = [int(x) for x in input().split(' ')]
original = []
compress = []
dif = []
for i in range(n):
	(o, c) = [int(x) for x in input().split(' ')]
	original.append(o)
	compress.append(c)
	dif.append(o - c)
dif.sort(reverse=True)
s = sum(original)
cant = 0
flag = True
if s <= m:
	print(0)
	flag = False
elif sum(compress) <= m:
	i = 0
	while s > m and len(dif) > 0:
		s -= dif[i]
		cant += 1
		i += 1
else:
	print(-1)
	flag = False
if flag:
	print(cant)


(n, m) = map(int, input().split())
l = []
c1 = 0
c2 = 0
for i in range(n):
	p = list(map(int, input().split()))
	c1 += p[0]
	c2 += p[1]
	p = p[0] - p[1]
	l.append(p)
if c1 < m:
	print(0)
elif c2 > m:
	print(-1)
elif c2 == m:
	print(n)
else:
	d = c1 - m
	l.sort(reverse=True)
	c = 0
	j = 0
	while d > 0:
		d -= l[j]
		j += 1
		c += 1
	print(c)


from sys import exit
(n, m) = map(int, input().split())
a = []
b = []
for _ in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	b.append(y)
offset = list(map(lambda x, y: x - y, a, b))
if sum(b) > m:
	print(-1)
else:
	offset = sorted(offset, reverse=True)
	t = sum(a) - m
	cnt = 0
	s = 0
	for x in offset:
		if s >= t:
			print(cnt)
			exit(0)
		else:
			cnt += 1
			s += x
	if s >= t:
		print(cnt)


(n, m) = map(int, input().split())
ai = []
bi = []
compression = []
no_of_compressions = 0
original_size = 0
compressed_size = 0
for i in range(0, n):
	(a, b) = map(int, input().split())
	ai.append(a)
	bi.append(b)
	compression.append(a - b)
compression.sort(reverse=True)
original_size = sum(ai)
compressed_size = sum(bi)
if compressed_size > m:
	no_of_compressions = -1
else:
	for i in compression:
		if original_size <= m:
			break
		else:
			no_of_compressions += 1
			original_size = original_size - i
print(no_of_compressions)


(n, m) = map(int, input().strip().split())
arr = []
for _ in range(n):
	(a, b) = map(int, input().strip().split())
	arr.append([a, b])
arr.sort(key=lambda x: x[0] - x[1], reverse=True)
s = sum([x[0] for x in arr])
if s <= m:
	print(0)
else:
	for i in range(n):
		s -= arr[i][0] - arr[i][1]
		if s <= m:
			print(i + 1)
			break
	else:
		print(-1)


(n, m) = map(int, input().split())
a = []
b = []
c = []
d = 0
for i in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	b.append(y)
if sum(b) > m:
	print('-1')
elif sum(a) <= m:
	print('0')
else:
	for i in range(n):
		j = a[i] - b[i]
		c.append(j)
	c.sort()
	c.reverse()
	k = sum(a)
	for i in range(n):
		k = k - c[i]
		d = d + 1
		if k <= m:
			break
	print(d)


(n, m) = [int(i) for i in input().strip().split(' ')]
diff = []
summing = 0
counting = 0
for i in range(n):
	(a, b) = [int(i) for i in input().strip().split(' ')]
	summing += a
	diff.append(a - b)
diff.sort()
while diff != [] and summing > m:
	t = diff.pop()
	summing = summing - t
	counting += 1
if summing > m:
	print(-1)
else:
	print(counting)


(n, c) = map(int, input().split())
(q, r, o) = ([], 0, 0)
for i in range(n):
	(a, b) = map(int, input().split())
	r += b
	o += a
	q.append(a - b)
q.sort(reverse=True)
if r > c:
	print(-1)
else:
	i = 0
	while o > c:
		o -= q[i]
		i += 1
	print(i)


from functools import cmp_to_key

class song:

	def __init__(self, x, y):
		self.uc = x
		self.c = y

	def __str__(self):
		return '[{}, {}]'.format(self.uc, self.c)

def mycomp(a, b):
	if a.uc > b.uc:
		return 1
	elif a.uc == b.uc:
		if a.c > b.c:
			return -1
		elif a.c == b.c:
			return 0
		else:
			return 1
	else:
		return -1
mycomp = cmp_to_key(mycomp)

def compress(songs, m):
	ucsum = 0
	ccsum = 0
	for k in songs:
		ucsum += k.uc
		ccsum += k.c
	if ccsum > m:
		return -1
	if ucsum <= m:
		return 0
	diff = []
	for k in songs:
		diff.append(k.uc - k.c)
	diff.sort(reverse=True)
	for j in range(len(diff)):
		if ucsum <= m:
			return j
		else:
			ucsum -= diff[j]
	if ucsum <= m:
		return len(songs)
(n, m) = tuple(map(int, input().split()))
songs = []
for i in range(n):
	(x, y) = tuple(map(int, input().split()))
	songs.append(song(x, y))
ans = compress(songs, m)
print(ans)


import sys
(n, total) = [int(x) for x in input().split(' ')]
diff = []
sum = 0
for i in range(n):
	(a, b) = [int(x) for x in input().split(' ')]
	diff.append(a - b)
	sum = sum + a
if sum <= total:
	print(0)
	sys.exit(0)
diff.sort(reverse=True)
count = 0
for i in range(n):
	sum = sum - diff[i]
	count = count + 1
	if sum <= total:
		print(count)
		sys.exit(0)
print(-1)
sys.exit(0)


x = [int(x) for x in input().split()]
m = x[1]
n = x[0]
d = []
t = 0
s = []
for i in range(n):
	p = [int(x) for x in input().split()]
	d.append(p)
for i in range(n):
	k = d[i][0] - d[i][1]
	t += d[i][0]
	s.append(k)
o = t - m
s = sorted(s, key=int, reverse=True)
if sum(s) < o:
	print(-1)
else:
	u = 0
	i = 0
	while u < o:
		u += s[i]
		i += 1
	print(i)


(n, m) = map(int, input().split())
s = []
mas = 0
mis = 0
for _ in range(n):
	tmp = list(map(int, input().split()))
	mas += tmp[0]
	mis += tmp[1]
	s.append(tmp)
d = mas - m
dif = []
for item in s:
	dif.append(item[0] - item[1])
dif.sort(reverse=True)
if mas <= m:
	print('0')
elif mis > m:
	print('-1')
elif d < max(dif):
	print('1')
else:
	cnt = 0
	i = 0
	while d > 0:
		d -= dif[i]
		i += 1
		cnt += 1
	print(cnt)


i = lambda : map(int, input().split())
(n, m) = i()
r = []
for _ in range(n):
	(a, b) = i()
	m -= b
	r += [a - b]
for iv in sorted(r):
	if m >= iv:
		m -= iv
		n -= 1
print(-1 if m < 0 else n)


(n, m) = [int(x) for x in input().split()]
d = []
s1 = 0
s2 = 0
for i in range(n):
	(x, y) = [int(x) for x in input().split()]
	s1 += x
	s2 += y
	d.append([x, y])
if s1 <= m:
	print(0)
elif s2 > m:
	print(-1)
else:
	d.sort(reverse=True, key=lambda x: x[0] - x[1])
	ans = s1 - m
	k = 0
	while ans > 0:
		ans -= d[k][0] - d[k][1]
		k += 1
	print(k)


(n, m) = map(int, input().split())
v = []
for i in range(n):
	(a, b) = map(int, input().split())
	v.append((a, b))
v = sorted(v, key=lambda e: e[0] - e[1], reverse=True)
tot = sum((e[0] for e in v))
c = 0
i = 0
while i < n and tot > m:
	e = v[i]
	diff = e[0] - e[1]
	tot -= diff
	c += 1
	i += 1
if tot > m:
	print(-1)
else:
	print(c)


(n, m) = map(int, input().split())
z = []
s = 0
e = 0
r = 0
for i in range(n):
	(a, b) = map(int, input().split())
	z.append(a - b)
	s = s + a
	e = e + b
z.sort()
z.reverse()
while r < n and s > m:
	s = s - z[r]
	r = r + 1
if e > m:
	print(-1)
else:
	print(r)


(n, m) = map(int, input().split())
l = []
s = 0
sl = 0
for i in range(n):
	(a, b) = map(int, input().split())
	s += a
	sl += a - b
	l.append(a - b)
if s - sl > m:
	print(-1)
elif s - sl == m:
	print(n)
else:
	l.sort()
	i = -1
	while s > m:
		s -= l[i]
		i -= 1
	print(abs(i) - 1)


(n, m) = map(int, input().split())
(l, r, d) = ([], [], [])
for i in range(n):
	(a, b) = map(int, input().split())
	l.append(a)
	r.append(b)
	d.append(a - b)
a = sum(l)
d.sort(reverse=True)
ans = 0
for i in range(n):
	if a > m and d[i] > 0:
		a -= d[i]
		ans += 1
	else:
		break
if a <= m:
	print(ans)
else:
	print(-1)


import sys
(n, m) = (int(s) for s in input().split())
v = []
s = 0
for i in range(n):
	(a, b) = (int(s) for s in input().split())
	v.append(a - b)
	s += a
v.sort()
sol = 0
k = len(v) - 1
while s > m and k >= 0:
	s -= v[k]
	k -= 1
	sol += 1
if s <= m:
	print(sol)
else:
	print(-1)


(n, m) = map(int, input().split())
(l, li, l1) = ([], [], [])
(sl, sli) = (0, 0)
for i in range(n):
	(x, y) = map(int, input().split())
	sl += x
	sli += y
	l.append(x)
	li.append(y)
	l1.append(x - y)
if sl <= m:
	print(0)
elif sli > m:
	print(-1)
else:
	tar = sl - m
	l1.sort()
	l1 = l1[::-1]
	s = 0
	for j in range(n):
		s += l1[j]
		if s >= tar:
			print(j + 1)
			break


import collections, atexit, math, sys
from functools import cmp_to_key
sys.setrecursionlimit(1000000)

def getIntList():
	return list(map(int, input().split()))
import bisect
try:
	import numpy

	def dprint(*args, **kwargs):
		print(*args, **kwargs, file=sys.stderr)
	dprint('debug mode')
except ModuleNotFoundError:

	def dprint(*args, **kwargs):
		pass

def memo(func):
	cache = {}

	def wrap(*args):
		if args not in cache:
			cache[args] = func(*args)
		return cache[args]
	return wrap

@memo
def comb(n, k):
	if k == 0:
		return 1
	if n == k:
		return 1
	return comb(n - 1, k - 1) + comb(n - 1, k)
inId = 0
outId = 0
if inId > 0:
	dprint('use input', inId)
	sys.stdin = open('input' + str(inId) + '.txt', 'r')
if outId > 0:
	dprint('use output', outId)
	sys.stdout = open('stdout' + str(outId) + '.txt', 'w')
	atexit.register(lambda : sys.stdout.close())
(N, M) = getIntList()
total = 0
zz = [0 for i in range(N)]
for i in range(N):
	(a, b) = getIntList()
	total += a
	zz[i] = a - b
zz.sort(reverse=True)
if total <= M:
	print(0)
	return
for i in range(N):
	total -= zz[i]
	if total <= M:
		print(i + 1)
		return
print(-1)


(n, m) = map(int, input().strip().split())
(initial, final, arr) = ([], [], [])
for i in range(n):
	(a, b) = map(int, input().strip().split())
	initial.append(a)
	final.append(b)
	arr.append(a - b)
if sum(final) > m:
	print(-1)
elif sum(initial) <= m:
	print(0)
else:
	limit = sum(initial) - m
	count = 0
	arr.sort(reverse=True)
	value = 0
	for i in arr:
		value += i
		count += 1
		if value >= limit:
			break
	print(count)


str1 = input()
(tim, how) = map(int, str1.split())
datas = list(range(tim))
ind = 0
org_how = 0
zip_how = 0
while tim != 0:
	tim = tim - 1
	str1 = input()
	(x, y) = map(int, str1.split())
	datas[ind] = x - y
	org_how = org_how + x
	zip_how = zip_how + y
	ind = ind + 1
if zip_how > how:
	print('-1')
elif org_how <= how:
	print('0')
else:
	datas.sort(reverse=True)
	cnt = 0
	now_ind = 0
	while org_how > how:
		cnt = cnt + 1
		org_how = org_how - datas[now_ind]
		now_ind = now_ind + 1
	print(str(cnt))


(n, m) = map(int, input().split())
sumb = 0
suma = 0
s = []
sumschat = 0
k = 0
for i in range(n):
	(a, b) = map(int, input().split())
	s.append(a - b)
	sumb = sumb + b
	suma = suma + a
if sumb > m:
	print(-1)
else:
	i = 0
	s.sort(reverse=True)
	while sumschat < suma - m:
		sumschat = sumschat + s[i]
		i = i + 1
		k = k + 1
	print(k)


t = 1
for test in range(1, t + 1):
	(n, m) = map(int, input().split())
	a = 0
	b = 0
	c = []
	for i in range(n):
		(l, r) = map(int, input().split())
		a += l
		b += r
		c.append(l - r)
	if b > m:
		print(-1)
	else:
		c.sort(reverse=True)
		diff = a - m
		ans = 0
		ind = 0
		while diff > 0:
			diff -= c[ind]
			ind += 1
			ans += 1
		print(ans)


(n, m) = map(int, input().split())
suma = 0
sumb = 0
l = []
for _ in range(n):
	(a, b) = map(int, input().split())
	suma += a
	sumb += b
	l.append(a - b)
if suma <= m:
	print(0)
elif sumb > m:
	print(-1)
else:
	l.sort(reverse=True)
	x = suma - m
	ans = 0
	i = 0
	while x > 0:
		ans += 1
		x = x - l[i]
		i += 1
	print(ans)


readints = lambda : map(int, input().strip('\n').split())
(n, m) = readints()
btot = 0
vec = []
for _ in range(n):
	(a, b) = readints()
	btot += b
	d = a - b
	vec.append((d, a, b))
if btot > m:
	print(-1)
else:
	vec = list(reversed(sorted(vec)))
	sz = 0
	for (d, a, b) in vec:
		sz += a
	i = 0
	while i < n and sz > m:
		sz -= vec[i][1]
		sz += vec[i][2]
		i += 1
	print(i)


(n, m) = map(int, input().split(' '))
a = []
b = []
e = []
for i in range(n):
	(c, d) = map(int, input().split(' '))
	a.append(c)
	b.append(d)
	e.append(c - d)
if sum(b) > m:
	print(-1)
elif sum(b) == m:
	print(n)
else:
	e = sorted(e)
	v = sum(a) - m
	h = 0
	if v <= 0:
		print(0)
	else:
		for i in reversed(e):
			v = v - i
			h = h + 1
			if v <= 0:
				print(h)
				break


(songs_count, flash) = [int(x) for x in input().split()]
songs = []
for _ in range(songs_count):
	(a, b) = [int(x) for x in input().split()]
	songs.append((a, b, a - b))
songs_sum = sum((song[0] for song in songs))
if songs_sum <= flash:
	print(0)
elif sum((song[1] for song in songs)) > flash:
	print(-1)
else:
	zip_count = 0
	w = sorted([x[2] for x in songs], reverse=True)
	delta = songs_sum - flash
	for z in w:
		zip_count += 1
		delta -= z
		if delta <= 0:
			break
	print(zip_count)


import sys

def input():
	return sys.stdin.readline()
(n, mx) = map(int, input().split())
l = []
sm = 0
for _ in range(n):
	(x, y) = map(int, input().split())
	l.append([x, y, x - y])
	sm += y
if sm > mx:
	print(-1)
else:
	l.sort(key=lambda x: -x[2])
	cnt = n
	while sm <= mx:
		try:
			x = l.pop()
			cnt -= 1
			sm += x[2]
		except:
			sm += 1
			cnt -= 1
	if cnt < 0:
		print(0)
	else:
		print(cnt + 1)


(n, m) = (int(_) for _ in input().split(' '))
a = list()
b = list()
diff = list()
for _ in range(0, n):
	(ai, bi) = (int(_) for _ in input().split(' '))
	a.append(ai)
	b.append(bi)
	diff.append(ai - bi)
if sum(b) > m:
	print('-1')
else:
	size = sum(a)
	diff.sort(reverse=True)
	num = 0
	while size > m:
		size -= diff[num]
		num += 1
	print(num)


from collections import defaultdict
import string
(n, m) = [int(i) for i in input().split()]
x = []
s = 0
d = 0
for i in range(n):
	(a, b) = [int(i) for i in input().split()]
	c = a - b
	s += a
	d += b
	x.append((a, b, c))
x.sort(key=lambda f: -f[2])
if d > m:
	print(-1)
else:
	c = 0
	for i in range(n):
		if s <= m:
			print(c)
			exit(0)
		else:
			s -= x[i][2]
			c += 1
	print(c)


(n, m) = [int(i) for i in input().split()]
A = [[int(i) for i in input().split()] for j in range(n)]
suma = 0
D = []
for (a, b) in A:
	suma += a
	D.append(a - b)
D.sort(reverse=True)
ans = 0
for d in D:
	if suma <= m:
		break
	suma -= d
	ans += 1
if suma > m:
	ans = -1
print(ans)


i = lambda : map(int, input().split())
(n, m) = i()
difference = []
sumOverall = 0
sumCompressed = 0
for _ in range(n):
	(first, last) = i()
	sumOverall += first
	sumCompressed += last
	difference.append(first - last)
if sumOverall <= m:
	print(0)
elif sumCompressed > m:
	print(-1)
else:
	count = 0
	difference.sort(reverse=True)
	for i in range(len(difference)):
		sumOverall -= difference[i]
		count += 1
		if sumOverall <= m:
			break
	print(count)


(n, m) = map(int, input().split())
a = []
b = []
for i in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	b.append(y)
if sum(b) > m:
	print(-1)
else:
	c = [a[i] - b[i] for i in range(len(a))]
	c.sort()
	s = sum(b)
	res = 0
	for i in c:
		if s + i <= m:
			res += 1
			s += i
		else:
			break
	print(n - res)


import sys
(n, m) = map(int, input().split())
a = list()
b = list()
for i in range(n):
	s = list(map(int, input().split()))
	a.append(s[0] - s[1])
	b.append(s[0])
a.sort(reverse=True)
count = sum(b)
if count <= m:
	print(0)
	sys.exit()
for i in range(len(a)):
	count = count - a[i]
	if count <= m:
		print(i + 1)
		break
else:
	print(-1)


(n, m) = map(int, input().split())
ss = 0
c = []
for i in range(0, n):
	(x, y) = map(int, input().split())
	ss += x
	c.append(x - y)
c.sort()
c.reverse()
ans = 0
for i in range(0, n):
	if ss <= m:
		break
	ss -= c[i]
	ans += 1
if ss <= m:
	print(ans)
else:
	print(-1)


(n, m) = map(int, input().split())
a = 0
b = 0
c = []
jap = 0
for i in range(n):
	(A, B) = map(int, input().split())
	C = A - B
	c.append(C)
	a += A
	b += B
	if b > m:
		print(-1)
		jap = 1
		break
c.sort()
if jap == 0:
	if a < m:
		print(0)
	elif b > m:
		print(-1)
	else:
		k = 0
		counter = 0
		while a > m:
			counter += 1
			a -= c.pop()
		print(counter)


(n, size) = map(int, input().split())
li = []
for _ in range(n):
	(a, b) = map(int, input().split())
	li.append((a, b, a - b))
sum = 0
sum1 = 0
for i in range(n):
	sum += li[i][1]
	sum1 += li[i][0]
if sum > size:
	print(-1)
	return
if sum1 <= size:
	print(0)
	return
li.sort(key=lambda x: x[2], reverse=True)
dif = sum1 - size
count = 0
for i in range(n):
	if dif <= 0:
		break
	dif -= li[i][2]
	count += 1
print(count)


(n, m) = input().split()
n = int(n)
m = int(m)
max_a = 0
all_a = []
all_b = 0
for i in range(n):
	(a, b) = input().split()
	a = int(a)
	b = int(b)
	all_b += b
	all_a.append(a - b)
	if a > max_a:
		max_a = a
all_a.sort()
i = 0
k = n
if all_b > m:
	print(-1)
else:
	while all_b <= m and i < len(all_a):
		all_b += all_a[i]
		i += 1
		if all_b <= m:
			k -= 1
	print(k)


(n, m) = (int(i) for i in input().split(' '))
suma = 0
sumb = 0
difference = []
songs = 0
for x in range(n):
	(a, b) = (int(p) for p in input().split(' '))
	suma = suma + a
	sumb = sumb + b
	difference.append(a - b)
x = suma - m
difference.sort(reverse=True)
if sumb > m:
	print(-1)
elif x <= 0:
	print(0)
else:
	i = 0
	while x > 0:
		x = x - difference[i]
		songs = songs + 1
		i = i + 1
	print(songs)


(n, si) = map(int, input().split())
e = []
s = 0
q = 0
for i in range(n):
	(a, b) = map(int, input().split())
	e.append(a - b)
	s = s + a
	q = q + b
e.sort()
e.reverse()
x = 0
while x < n and s > si:
	s = s - e[x]
	x = x + 1
if q > si:
	print(-1)
else:
	print(x)


(n, m) = map(int, input().split())
a = []
b = []
non_compressed = 0
compressed = 0
saves = []
for i in range(n):
	(ai, bi) = map(int, input().split())
	non_compressed += ai
	compressed += bi
	saves.append(ai - bi)
if compressed > m:
	print(-1)
else:
	saves.sort(reverse=True)
	z = 0
	while non_compressed > m:
		non_compressed -= saves[z]
		z = z + 1
	print(z)


import sys
import math
import itertools
import collections

def getdict(n):
	d = {}
	if type(n) is list or type(n) is str:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def wr(arr):
	return ' '.join(map(str, arr))

def revn(n):
	return int(str(n)[::-1])

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base=3):
	newnumber = ''
	while number > 0:
		newnumber = str(number % base) + newnumber
		number //= base
	return newnumber
(n, m) = mi()
d = [0] * n
sa = sb = 0
for i in range(n):
	(a, b) = mi()
	d[i] = a - b
	sa += a
	sb += b
if sa <= m:
	print(0)
elif sb > m:
	print(-1)
else:
	d.sort(reverse=True)
	ans = 0
	i = 0
	while sa > m:
		sa -= d[i]
		ans += 1
		i += 1
	print(ans)


(n, m) = map(int, input().split())
(a, b) = ([], [])
for _ in range(n):
	ab = list(map(int, input().split()))
	a.append(ab[0])
	b.append(ab[1])
diffs = sorted([a - b for (a, b) in zip(a, b)], reverse=True)
ans = 0
s = sum(a)
for d in diffs + [0]:
	if s <= m:
		print(ans)
		exit(0)
	s -= d
	ans += 1
print(-1)


(x, y) = map(int, input().split())
count1 = 0
li1 = []
sum1 = 0
for _ in range(x):
	li = list(map(int, input().split()))
	sum1 += li[0]
	li1.append(li[0] - li[1])
if sum1 <= y:
	print(0)
else:
	li1.sort()
	for i in range(x - 1, -1, -1):
		sum1 -= li1[i]
		count1 += 1
		if sum1 <= y:
			print(count1)
			break
	else:
		print(-1)


(n, m) = map(int, input().split())
sum_s = 0
r = []
c = 0
e = 0
for i in range(n):
	(a, b) = map(int, input().split())
	sum_s = sum_s + a
	subs = a - b
	r.append(subs)
	e = e + b
y = sum_s
r.sort()
r.reverse()
j = 0
if e <= m:
	while y > m and j < n:
		y = y - r[j]
		j = j + 1
		c = c + 1
	print(c)
else:
	print('-1')


import heapq
(n, c) = map(int, input().split())
a = []
b = []
d = []
asum = 0
bsum = 0
dsum = 0
count = 0
for i in range(n):
	(ai, bi) = map(int, input().split())
	asum = asum + ai
	bsum = bsum + bi
	heapq.heappush(d, bi - ai)
dsum = asum - c
if bsum > c:
	print(-1)
else:
	for i in range(n):
		if dsum > 0:
			dsum = dsum + heapq.heappop(d)
			count = count + 1
		else:
			break
	print(count)


(n, m) = map(int, input().split())
a = []
b = []
diff = []
for i in range(n):
	(l, p) = map(int, input().split())
	a.append(l)
	b.append(p)
	diff.append(l - p)
tot = sum(a) - m
if tot <= 0:
	print(0)
else:
	diff.sort(reverse=True)
	count = 0
	for i in diff:
		tot -= i
		count += 1
		if tot <= 0:
			break
	if tot <= 0:
		print(count)
	else:
		print(-1)


import math
n = list(map(int, input().split()))
a = []
b = 0
f = 0
h = 0
for i in range(n[0]):
	c = list(map(int, input().split()))
	b += c[1]
	a.append(c[0] - c[1])
a.sort(reverse=True)
ac = a.copy()
if b > n[1]:
	print(-1)
elif sum(a) + b <= n[1]:
	print(0)
else:
	while len(a) != 1:
		if b + sum(ac) - sum(a[:math.floor(len(a) / 2)]) - f <= n[1]:
			a = a[:math.floor(len(a) / 2)]
		else:
			f += sum(a[:math.floor(len(a) / 2)])
			h += math.floor(len(a) / 2)
			a = a[math.floor(len(a) / 2):]
	print(int(h + 1))


from sys import stdout
(n, m) = map(int, input().split())
tab = []
ecart = []
s = 0
for i in range(n):
	(l, r) = map(int, input().split())
	s += l
	tab.append([l, r])
	ecart.append([l - r, i])
if s <= m:
	print(0)
else:
	ecart.sort()
	v = n
	n -= 1
	k = ecart[n][1]
	resp = 0
	found = False
	while True:
		if s - tab[k][0] + tab[k][1] <= m:
			print(resp + 1)
			found = True
			break
		resp += 1
		s = s - tab[k][0] + tab[k][1]
		n -= 1
		k = ecart[n][1]
		if resp == v:
			break
	if not found:
		print(-1)


(n, m) = map(int, input().split())
z = 0
d = []
for i in range(n):
	(a, b) = map(int, input().split())
	z += a
	d.append(a - b)
d.sort(reverse=True)
tar = z - m
if tar <= 0:
	print(0)
	exit()
s = 0
for i in range(n):
	s += d[i]
	if s >= tar:
		print(i + 1)
		exit()
print(-1)


(n, m) = map(int, input().split())
a = []
b = []
c = []
z = 0
z1 = 0
for i in range(0, n):
	(x, y) = map(int, input().split())
	a.append(x)
	b.append(y)
	c.append(x - y)
	z += y
	z1 += x
c.sort()
if z > m:
	print(-1)
elif z1 <= m:
	print(0)
else:
	for i in range(n, 0, -1):
		z1 -= c[i - 1]
		if z1 <= m:
			break
	print(n - i + 1)


import sys
(n, m) = map(int, sys.stdin.readline().split())
arr = []
sigma = 0
for _ in range(n):
	(a, b) = map(int, sys.stdin.readline().split())
	arr.append((a, b))
	sigma += a
if sigma <= m:
	print(0)
else:
	arr.sort(key=lambda x: -1 * (x[0] - x[1]))
	temp = sigma - m
	ans = 0
	for i in arr:
		if temp <= 0:
			break
		temp -= i[0] - i[1]
		ans += 1
	if temp <= 0:
		print(ans)
	else:
		print(-1)


l1 = list(map(int, input().split()))
n = l1[0]
m = l1[1]
l = []
sum1 = 0
sum2 = 0
for i in range(n):
	l1 = list(map(int, input().split()))
	l.append(l1[0] - l1[1])
	sum1 = sum1 + l1[0]
	sum2 = sum2 + l1[1]
if sum2 > m:
	print(-1)
else:
	l.sort()
	l = l[::-1]
	diff = sum1 - m
	i = 0
	while diff > 0:
		diff -= l[i]
		i += 1
	print(i)


(n, m) = map(int, input().split())
arr = []
summ = 0
for i in range(n):
	arr1 = list(map(int, input().split()))
	arr.append(arr1[0] - arr1[1])
	summ += arr1[0]
arr.sort(reverse=True)
flag = 0
for i in range(n):
	if summ <= m:
		flag = 1
		print(i)
		break
	summ -= arr[i]
if flag == 0 and summ <= m:
	print(n)
elif summ > m:
	print(-1)


from sys import stdin, stdout
input = lambda : stdin.readline().rstrip()
write = stdout.write

def main():
	(N, M) = map(int, input().split())
	AB = [tuple(map(int, input().split())) for _ in [0] * N]
	(before, after) = (0, 0)
	compress = []
	for (a, b) in AB:
		before += a
		after += b
		compress.append(a - b)
	if M < after:
		print(-1)
		return
	elif M >= before:
		print(0)
		return
	compress.sort(reverse=1)
	ans = 0
	for i in range(N):
		before -= compress[i]
		ans += 1
		if M >= before:
			break
	print(ans)
main()


def solve(a, b, n, m):
	total = sum(a)
	total = total - m
	diff = []
	for i in range(n):
		diff.append(a[i] - b[i])
	diff.sort(reverse=True)
	summ = 0
	cnt = 0
	for i in range(n):
		if summ < total:
			summ += diff[i]
			cnt += 1
		else:
			print(cnt)
			return
	if summ >= total:
		print(cnt)
	else:
		print(-1)
(n, m) = map(int, input().split())
a = []
b = []
for _ in range(n):
	(l, r) = map(int, input().split())
	a.append(l)
	b.append(r)
solve(a, b, n, m)


n = input().split(' ')
t = z = 0
q = []
for i in range(int(n[0])):
	p = input().split(' ')
	t += int(p[1])
	z += int(p[0])
	q.append(int(p[0]) - int(p[1]))
if t > int(n[1]):
	print(-1)
elif z <= int(n[1]):
	print(0)
else:
	w = z - int(n[1])
	if max(q) >= w:
		print(1)
	else:
		q.sort(reverse=True)
		e = q[0]
		t = 1
		while e < w:
			e += q[t]
			t += 1
		print(t)


q = input()
(n, m) = q.split()
n = int(n)
m = int(m)
sh = 0
s = 0
s2 = 0
k = 0
q = []
w = []
for i in range(n):
	pack = input()
	(a, b) = pack.split()
	a = int(a)
	b = int(b)
	c = a - b
	q.append((a, b, c))
for i in range(len(q)):
	w.append(q[i][2])
w.sort(reverse=True)
for i in range(len(q)):
	s = s + q[i][1]
for i in range(len(q)):
	s2 = s2 + q[i][0]
if s > m:
	print('-1')
else:
	while True:
		if s2 <= m:
			break
		s2 -= w[k]
		k += 1
		sh += 1
	print(sh)


(n, m) = map(int, input().split())
sum_ = 0
q = []
for i in range(n):
	(a, b) = map(int, input().split())
	sum_ += a
	q.append(a - b)
q.sort(reverse=True)
i = 0
c = 0
while i < len(q) and sum_ > m:
	sum_ -= q[i]
	c += 1
	i += 1
if sum_ <= m:
	print(c)
else:
	print(-1)


(n, m) = map(int, input().split())
arr = []
for i in range(n):
	(x, y) = map(int, input().split())
	arr.append((x, y))
arr = sorted(arr, key=lambda x: x[0] - x[1], reverse=True)
curr_req = 0
ans = 0
for i in arr:
	curr_req += i[0]
for i in arr:
	if curr_req > m:
		curr_req += i[1] - i[0]
		ans += 1
	else:
		break
if curr_req <= m:
	print(ans)
else:
	print(-1)


(n, m) = map(int, input().split())
final = []
for i in range(n):
	(a, b) = map(int, input().split())
	final.append([a, b])
final.sort(key=lambda x: x[0] - x[1])
s1 = 0
s2 = 0
for i in final:
	s2 += i[1]
	s1 += i[0]
if s2 > m:
	print(-1)
elif s1 <= m:
	print(0)
else:
	i = n - 1
	count = 0
	while s1 > m:
		s1 = s1 - (final[i][0] - final[i][1])
		count += 1
		i -= 1
	print(count)


(n, m) = [int(i) for i in input().split()]
sum = 0
l = []
suma = 0
for i in range(n):
	(a, b) = [int(i) for i in input().split()]
	l.append(a - b)
	sum = sum + b
	suma = suma + a
if sum > m:
	print(-1)
else:
	c = 0
	d = suma
	l = sorted(l, reverse=True)
	for j in range(0, len(l)):
		if d <= m:
			break
		else:
			d = d - l[j]
			c = c + 1
	print(c)


import sys
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
(N, M) = MAP()
diff = [0] * N
sm = 0
for i in range(N):
	(a, b) = MAP()
	diff[i] = a - b
	sm += a
if sm <= M:
	print(0)
	exit()
diff.sort(reverse=1)
ans = 0
for d in diff:
	sm -= d
	ans += 1
	if sm <= M:
		print(ans)
		exit()
print(-1)


(n, m) = [int(x) for x in input().split()]
N = []
M = []
D = []
for i in range(n):
	(a, b) = [int(x) for x in input().split()]
	N.append(a)
	M.append(b)
	D.append(a - b)
if sum(M) > m:
	print(-1)
else:
	D.sort(reverse=True)
	count = 0
	s = 0
	t = sum(N) - m
	while s < t:
		s += D[count]
		count += 1
	print(count)


(n, m) = map(int, input().split())
a = []
b = []
c = []
cnt = 0
F = 0
for j in range(n):
	(x, y) = map(int, input().split())
	b.append(y)
	a.append(x)
	c.append(x - y)
bsum = sum(b)
asum = sum(a)
c.sort(key=None, reverse=True)
if bsum > m:
	print(-1)
if asum > m and bsum <= m:
	for i in range(n):
		if c[0] == 0:
			continue
		cnt += 1
		asum = asum - c[i]
		if asum <= m:
			print(cnt)
			F = 1
			break
if asum <= m and F == 0:
	print(0)


def main():
	(n, m) = [int(x) for x in input().split()]
	s = []
	for _ in range(n):
		(a, b) = [int(x) for x in input().split()]
		s.append((a, b))
	sum_a = sum((x[0] for x in s))
	sum_b = sum((x[1] for x in s))
	if sum_b > m:
		print(-1)
		return
	s.sort(key=lambda x: x[0] - x[1], reverse=True)
	i = 0
	while sum_a > m:
		sum_a -= s[i][0] - s[i][1]
		i += 1
	print(i)
main()


import sys, math
(n, m) = map(int, input().split())
diff = []
aktsum = 0
for i in range(n):
	(x, y) = map(int, input().split())
	diff.append(x - y)
	aktsum += x
if len(diff) == 0 or aktsum <= m:
	print(0)
	sys.exit()
else:
	ziel = aktsum - m
diff.sort()
aktsum = 0
for (i, e) in enumerate(reversed(diff)):
	aktsum += e
	if aktsum >= ziel:
		print(i + 1)
		sys.exit()
print(-1)


(n, m) = map(int, input().split())
(tot, v) = (0, [])
for i in range(n):
	(a, b) = map(int, input().split())
	tot += a
	v.append(a - b)
v.sort(reverse=True)
cnt = 0
for i in v:
	if tot > m:
		tot -= i
		cnt += 1
	else:
		break
print(cnt if tot <= m else -1)


(n, k) = map(int, input().split())
diff = []
nc_sum = 0
for i in range(n):
	(a, b) = map(int, input().split())
	diff.append(a - b)
	nc_sum += a
diff.sort(reverse=True)
i = -1
while k < nc_sum and i < n - 1:
	i += 1
	nc_sum -= diff[i]
if k < nc_sum:
	print(-1)
else:
	print(i + 1)


(n, m) = map(int, input().split())
Y = []
Sm = 0
for i in range(n):
	(x, y) = map(int, input().split())
	Sm += x
	Y.append(x - y)
Y.sort()
Y.reverse()
cur = 0
if Sm <= m:
	print(0)
	exit()
for i in range(n):
	Sm -= Y[i]
	cur += 1
	if Sm <= m:
		print(cur)
		exit()
print(-1)


(n, m) = map(int, input().split(' '))
diff = []
count = 0
total = 0
check = 0
for i in range(n):
	(a1, b1) = map(int, input().split(' '))
	total += a1
	check += b1
	diff.append(a1 - b1)
if check > m:
	print(-1)
	exit()
elif check == m:
	print(n)
	exit()
diff = sorted(diff, reverse=True)
for i in range(n):
	if total <= m:
		break
	total -= diff[i]
	count += 1
print(count)


(n, m) = map(int, input().split())
arr = []
total = 0
ans = 0
for i in range(n):
	(a, b) = map(int, input().split())
	arr.append(a - b)
	total += a
if total > m:
	arr = sorted(arr, reverse=True)
	j = 0
	while total > m and j < len(arr):
		total -= arr[j]
		j += 1
		ans += 1
	print(-1 if total > m else ans)
else:
	print(0)


(n, m) = [int(x) for x in input().split()]
a = 0
b = []
for x in range(n):
	(temp1, temp2) = [int(x) for x in input().split()]
	a += temp1
	b.append(temp1 - temp2)
b.sort(reverse=True)
if a <= m:
	print(0)
	exit(0)
for x in range(n):
	a -= b[x]
	if a <= m:
		print(x + 1)
		exit(0)
print(-1)


(n, m) = map(int, input().split())
a = []
b = []
diff = []
for _ in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	b.append(y)
	diff.append(x - y)
if sum(b) > m:
	print(-1)
elif sum(a) <= m:
	print(0)
else:
	count = 0
	diff.sort(reverse=True)
	sumA = sum(a)
	for x in diff:
		sumA -= x
		count += 1
		if sumA <= m:
			break
	print(count)


(n, m) = list(map(int, input().split()))
a = []
b = 0
c = 0
for i in range(n):
	(x, y) = list(map(int, input().split()))
	b += x
	c += y
	a.append(x - y)
if c > m:
	print(-1)
elif b <= m:
	print(0)
else:
	a.sort(reverse=True)
	for i in range(n):
		b -= a[i]
		if b <= m:
			print(i + 1)
			break


(n, m) = map(int, input().split())
diff = []
s = 0
for i in range(n):
	(a, b) = map(int, input().split())
	s += a
	diff.append(a - b)
diff = sorted(diff, reverse=True)
count = 0
for i in range(n):
	if s <= m:
		print(count)
		break
	else:
		s -= diff[i]
		count += 1
else:
	if s <= m:
		print(count)
	else:
		print(-1)


import sys
input = sys.stdin.readline

def read():
	return list(map(int, input().split()))
(n, m) = read()
a = []
for i in range(n):
	a.append(read())
sm = 0
cur = 0
for (x, y) in a:
	sm += y
	cur += x
if sm > m:
	print(-1)
	quit()
cnt = 0
a.sort(key=lambda x: -x[0] + x[1])
i = 0
while cur > m and i < n:
	cur -= a[i][0] - a[i][1]
	cnt += 1
	i += 1
print(cnt)


(n, bal) = map(int, input().split())
l = []
asum = 0
for i in range(n):
	(a, b) = map(int, input().split())
	asum += a
	l.append(a - b)
l.sort(reverse=True)
if asum <= bal:
	print(0)
	quit()
for i in range(len(l)):
	asum -= l[i]
	if asum <= bal:
		print(i + 1)
		quit()
print(-1)


import sys
s = input().split()
n = int(s[0])
m = int(s[1])
siz = 0
dec = [None] * n
for i in range(n):
	s = input().split()
	siz += int(s[0])
	dec[i] = int(s[1]) - int(s[0])
dec.sort()
if siz <= m:
	print(0)
	return
for i in range(n):
	siz += dec[i]
	if siz <= m:
		print(i + 1)
		return
print(-1)


import sys
(n, m) = map(int, input().split())
a = []
su = 0
kl = 0
for i in range(n):
	(z, x) = map(int, input().split())
	su += z
	kl += z - x
	a.append(z - x)

def prob(koli):
	suma = 0
	for i in range(koli):
		suma += a[i]
	return suma
if su - kl > m:
	print(-1)
	sys.exit()
else:
	a.sort()
	a.reverse()
	ko = 0
	l = 0
	r = n
	while l + 1 < r:
		p = (r + l) // 2
		if su - prob(p) > m:
			l = p
		else:
			r = p
if su - prob(l) <= m:
	print(l)
else:
	print(r)


from sys import stdin, stdout

def main():
	(n, m) = map(int, stdin.readline().split())
	A = [0 for _ in range(n)]
	B = [0 for _ in range(n)]
	for i in range(n):
		(A[i], B[i]) = map(int, stdin.readline().split())
	tot = sum(A)
	C = [A[i] - B[i] for i in range(n)]
	C.sort()
	i = 0
	while i < n and tot > m:
		i += 1
		tot -= C[-i]
	if tot <= m:
		stdout.write('{}'.format(i))
	else:
		stdout.write('-1')
main()


(n, m) = [int(x) for x in input().split()]
s = 0
li = []
diff = []
for i in range(n):
	(a, b) = [int(x) for x in input().split()]
	li.append(a)
	diff.append(a - b)
	s += b
if sum(li) <= m:
	print(0)
elif s > m:
	print(-1)
else:
	diff = sorted(diff, reverse=True)
	s1 = sum(li)
	c = 0
	for ele in diff:
		s1 -= ele
		c += 1
		if s1 <= m:
			break
	print(c)


(n, m) = map(int, input().split())
d = []
nsum = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	d.append(a - b)
	nsum += a
d.sort(reverse=True)
ans = 0
for i in range(n):
	if nsum <= m:
		print(ans)
		exit()
	else:
		nsum -= d[i]
		ans += 1
print(-1 if nsum > m else ans)


import heapq
import math
from collections import defaultdict, Counter
(n, m) = map(int, input().split())
l = []
for i in range(n):
	l.append(list(map(int, input().split())))
min_total = 0
curr_total = 0
for i in l:
	min_total += i[1]
	curr_total += i[0]
ans = 0
heap = []
for i in l:
	heapq.heappush(heap, -(i[0] - i[1]))
while curr_total > m and heap:
	z = abs(heapq.heappop(heap))
	ans += 1
	curr_total -= z
if curr_total <= m:
	print(ans)
else:
	print(-1)


(n, mx) = map(int, input().split())
l = []
sm = 0
for _ in range(n):
	(x, y) = map(int, input().split())
	l.append([x, y, x - y])
	sm += y
if sm > mx:
	print(-1)
else:
	l.sort(key=lambda x: -x[2])
	cnt = n
	while sm <= mx:
		try:
			x = l.pop()
			cnt -= 1
			sm += x[2]
		except:
			sm += 1
			cnt -= 1
	if cnt < 0:
		print(0)
	else:
		print(cnt + 1)


(n, m) = map(int, input().split())
s = 0
s1 = 0
a = []
for i in range(n):
	tmp = list(map(int, input().split()))
	a.append(tmp)
	s += tmp[1]
	s1 += tmp[0]
if s > m:
	print(-1)
else:
	a.sort(key=lambda x: x[0] - x[1], reverse=True)
	if s1 <= m:
		print(0)
		exit()
	diff = s1 - m
	res = 0
	for i in range(n):
		diff -= a[i][0] - a[i][1]
		if diff <= 0:
			print(res + 1)
			exit()
		res += 1
	print(res)


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
dif = []
sums = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	sums += a
	dif.append(a - b)
if sums - sum(dif) > m:
	count = -1
else:
	dif.sort()
	count = 0
	while sums > m:
		sums -= dif[-1 - count]
		count += 1
		if count == len(dif) - 1:
			if sums > m:
				count = n
			else:
				count = len(dif) - 1
			break
print(count)


(songs_number, flash_drive_capacity) = map(int, input().split())
deductions = []
total_size = 0
for i in range(songs_number):
	(size, compressed_size) = map(int, input().split())
	total_size += size
	deductions.append(size - compressed_size)
deductions.sort(reverse=True)
compressions_count = 0
i = 0
while i < songs_number and total_size > flash_drive_capacity:
	compressions_count += 1
	total_size -= deductions[i]
	i += 1
result = compressions_count if total_size <= flash_drive_capacity else -1
print(result)


(q, w) = map(int, input().split())
t = []
r = 0
for i in range(q):
	(d, f) = map(int, input().split())
	t.append(d - f)
	r += d
if r - sum(t) > w:
	print(-1)
else:
	t.sort(reverse=True)
	i = 0
	while r > w:
		r -= t[i]
		i += 1
	print(i)


(n, m) = map(int, input().split())
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
a = sorted(a, key=lambda x: x[1] - x[0])
(s, d) = (0, 0)
for i in range(len(a)):
	s += a[i][1]
	d += a[i][0]
if s > m:
	print(-1)
	exit()
if d <= m:
	print(0)
	exit()
(i, x) = (0, 0)
while x < d - m:
	x += a[i][0] - a[i][1]
	i += 1
print(i)


(n, m) = [int(x) for x in input().split()]
temp = []
tot = 0
for i in range(n):
	(a, b) = [int(x) for x in input().split()]
	temp.append(a - b)
	tot += a
ans = 0
if tot > m:
	temp.sort(reverse=True)
	for x in temp:
		tot -= x
		ans += 1
		if tot <= m:
			break
print(ans if tot <= m else -1)


(n, m) = map(int, input().split())
songs = []
sm = 0
maxComp = 0
cunt = 0
for i in range(n):
	(ini, fin) = map(int, input().split())
	sm += ini
	maxComp += fin
	songs.append(tuple((ini, fin)))
dif = [x[0] - x[1] for x in songs]
ans = sorted(dif, reverse=True)
if maxComp > m:
	print(-1)
else:
	for item in ans:
		if sm <= m:
			break
		cunt += 1
		sm -= item
	print(cunt)


def space(diff_list, compressed_total, m):
	diff_list = sorted(diff_list)
	count = len(diff_list)
	for d in diff_list:
		if m - d >= 0:
			m -= d
			count -= 1
		if not m:
			break
	return count
(n, m) = map(int, input().split())
inp = []
b_total = 0
h = []
for _ in range(n):
	(a, b) = map(int, input().split())
	b_total += b
	h.append(a - b)
ans = space(h, b_total, m - b_total) if m - b_total >= 0 else -1
print(ans)


import heapq

def main():
	(n, m) = map(int, input().split())
	heap = []
	size = 0
	compressed = 0
	for i in range(n):
		(a, b) = map(int, input().split())
		heapq.heappush(heap, (b - a, a, b))
		size += a
	while heap:
		if size <= m:
			break
		curr = heapq.heappop(heap)
		(a, b) = (curr[1], curr[2])
		size -= a
		size += b
		compressed += 1
	if size <= m:
		print(compressed)
		return
	print(-1)
main()


import sys
(n, m) = [int(x) for x in input().split()]
d = []
sum = 0
for i in range(n):
	(a, b) = [int(x) for x in input().split()]
	d.append((a - b, a, b))
	sum += a
d = sorted(d, reverse=True)
count = 0
for i in d:
	if sum <= m:
		print(count)
		sys.exit(0)
	sum -= i[0]
	count += 1
if sum <= m:
	print(count)
	sys.exit(0)
print(-1)


(n, m) = map(int, input().split())
l = []
(sumx, count) = (0, 0)
for _ in range(n):
	(a, b) = map(int, input().split())
	l.append(a - b)
	sumx += a
l.sort(reverse=True)
while count < n and sumx > m:
	sumx -= l[count]
	count += 1
print(count if sumx <= m else -1)


(n, m) = map(int, input().split())
contractions = 0
free_memory = []
occupied = 0
for i in range(n):
	(a, b) = map(int, input().split())
	contractions += b
	occupied += a
	free_memory.append(a - b)
if occupied <= m:
	print(0)
elif contractions > m:
	print(-1)
else:
	free_memory.sort(reverse=True)
	i = 0
	while m < occupied:
		occupied -= free_memory[i]
		i += 1
	print(i)


(n, m) = map(int, input().split())
c = [0] * n
sm = 0
for i in range(n):
	(f, s) = map(int, input().split())
	sm += f
	c[i] = s - f
c.sort()
ans = 0
for i in range(n):
	if sm <= m:
		break
	sm += c[i]
	ans += 1
if sm > m:
	print(-1)
else:
	print(ans)


(n, m) = map(int, input().split())
(s1, s2, l) = (0, 0, [])
for i in range(n):
	(a, b) = map(int, input().split())
	s1 += a
	s2 += b
	l.append(a - b)
if s2 > m:
	print(-1)
	exit(0)
if s1 <= m:
	print(0)
	exit(0)
l.sort(reverse=True)
c = 0
j = 0
while True:
	if s1 <= m:
		break
	s1 -= l[j]
	j += 1
	c += 1
print(c)


from pprint import pprint
from sys import exit
(n, m) = map(int, input().split())
arr = []
s1 = 0
s2 = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	arr.append([a, b, a - b])
	s1 += a
	s2 += b
if s1 <= m:
	print('0')
	exit(0)
if s2 > m:
	print('-1')
	exit(0)
arr.sort(key=lambda x: x[2], reverse=True)
ans = 0
i = 0
while s1 > m:
	s1 = s1 - arr[i][2]
	ans += 1
	i += 1
print(ans)


(n, m) = input().split()
n = int(n)
m = int(m)
songs = list()
for i in range(n):
	songs.append([int(c) for c in input().split()])

def sumList(lista, inx):
	sum = 0
	for i in range(len(lista)):
		sum += lista[i][inx]
	return sum
songs = sorted(songs, key=lambda x: x[1] - x[0])
suma = sumList(songs, 0)
for i in range(n):
	if suma <= m:
		print(i)
		exit()
	suma -= songs[i][0] - songs[i][1]
if suma <= m:
	print(n)
else:
	print(-1)


(n, m) = map(int, input().split())
(s1, s2) = (0, 0)
d = [0] * n
for i in range(n):
	(a, b) = map(int, input().split())
	s1 += a
	s2 += b
	d[i] = a - b
if s1 <= m:
	print(0)
elif s2 > m:
	print(-1)
else:
	i = s1 - m
	c = 0
	d.sort()
	while s1 > m:
		s1 -= d.pop()
		c += 1
	print(c)


(n, m) = map(int, input().split())
a = []
s = 0
for i in range(n):
	(x, y) = map(int, input().split())
	a.append(y - x)
	s += x
a.sort()
l = 0
ch = 0
f = 0
if s + sum(a) > m:
	f = 1
else:
	for i in a:
		if s + l > m:
			l += i
			ch += 1
		else:
			break
if f == 0:
	print(ch)
else:
	print(-1)


(n, m) = map(int, input().split())
L = []
sum = 0
for i in range(n):
	temp = list(map(int, input().split()))
	sum = sum + temp[0]
	L.append(temp)
if sum <= m:
	print(0)
else:
	D = []
	for i in range(n):
		D.append(L[i][0] - L[i][1])
	D.sort(reverse=True)
	i = 0
	count = 0
	while sum > m and i < n:
		sum = sum - D[i]
		count = count + 1
		i = i + 1
	if sum > m:
		print(-1)
	else:
		print(count)


(n, m) = map(int, input().split())
(a, b, r) = ([], [], [])
for i in range(n):
	(a_i, b_i) = map(int, input().split())
	a.append(a_i)
	b.append(b_i)
	r.append(a_i - b_i)
(sum_a, sum_b) = (sum(a), sum(b))
r.sort(reverse=True)
if sum_a <= m:
	print(0)
elif sum_b <= m:
	(i, k) = (0, 0)
	while sum_a > m:
		k += 1
		sum_a -= r[i]
		i += 1
	print(k)
else:
	print(-1)


(n, m) = map(int, input().split())
songs = []
size = 0
for i in range(n):
	(a, b) = map(int, input().split())
	c = a - b
	size += a
	songs.append(c)
songs.sort(reverse=True)
i = 0
while i < n and size > m:
	size -= songs[i]
	i += 1
print(-1 if size > m else i)


(n, m) = map(int, input().split())
total = 0
diff = []
for i in range(n):
	(a, b) = map(int, input().split())
	total += a
	diff.append(a - b)
diff.sort(reverse=True)
ans = 0
for i in range(n):
	if total <= m:
		break
	else:
		ans += 1
		total -= diff[i]
if total > m:
	print(-1)
else:
	print(ans)


(n, m) = list(map(lambda x: int(x), input().split()))
songs = []
curS = 0
for i in range(n):
	s = list(map(lambda x: int(x), input().split()))
	curS += s[0]
	songs.append(s[0] - s[1])
songs.sort()
songs = songs[::-1]
dif = curS - m
comps = 0
flag = False
while dif > 0:
	if comps >= n:
		flag = True
		break
	dif -= songs[comps]
	comps += 1
if flag:
	print(-1)
else:
	print(comps)


(n, m) = [int(x) for x in str(input()).split(' ', 1)]
a = []
b = []
d = []
for _ in range(n):
	(lx, rx) = [int(x) for x in str(input()).split(' ', 1)]
	a.append(lx)
	b.append(rx)
	d.append(lx - rx)
if sum(b) > m:
	print(-1)
	return
if sum(a) <= m:
	print(0)
	return
s = sum(a)
for (i, v) in enumerate(sorted(d, reverse=True)):
	s -= v
	if s <= m:
		print(i + 1)
		break


n = list(map(int, input().split()))
arr = []
rra = []
selisih = []
for i in range(n[0]):
	z = list(map(int, input().split()))
	arr.append(z[0])
	rra.append(z[1])
	selisih.append(abs(z[1] - z[0]))
selisih.sort()
if sum(rra) > n[1]:
	print(-1)
else:
	count = 0
	temp = sum(arr)
	while temp > n[1]:
		temp -= selisih[-1]
		del selisih[-1]
		count += 1
	print(count)


(n, m) = map(int, input().strip().split())
A = []
B = []
C = [0] * n
for _ in range(n):
	(a, b) = map(int, input().strip().split())
	A.append(a)
	B.append(b)
for i in range(n):
	C[i] = A[i] - B[i]
if sum(A) <= m:
	print(0)
elif sum(A) > m:
	if sum(B) > m:
		print(-1)
	else:
		cnt = sum(A) - m
		pnt = 0
		ans = 0
		for i in sorted(C, reverse=True):
			pnt += i
			ans += 1
			if pnt >= cnt:
				break
		print(ans)


(n, m) = map(int, input().split())
t = []
for i in range(n):
	(a, b) = map(int, input().split())
	t.append(a - b)
	m -= a
if m + sum(t) < 0:
	exit(print(-1))
t.sort()
ans = 0
while m < 0:
	m += t.pop()
	ans += 1
print(ans)


(n, s) = map(int, input().split())
d = [0] * n
(j, k, ans, sm) = (0, 0, 0, 0)
for i in range(n):
	(j, k) = map(int, input().split())
	d[i] = j - k
	sm += j
d.sort()
i = n - 1
while sm > s and i >= 0:
	sm -= d[i]
	i -= 1
	ans += 1
if sm > s:
	ans = -1
print(ans)


import sys
(n, m) = map(int, input().split())
files = []
size = 0
for i in range(n):
	file = tuple(map(int, input().split()))
	files.append(file)
	size += file[0]
if size <= m:
	print(0)
	sys.exit(0)
files.sort(key=lambda k: -(k[0] - k[1]))
for i in range(n):
	tempsize = size
	tempsize -= files[i][0]
	tempsize += files[i][1]
	if tempsize <= m:
		print(i + 1)
		sys.exit(0)
	size = tempsize
print(-1)


(n, m) = map(int, input().split())
a = []
b = []
c = []
for k in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	b.append(y)
	c.append(x - y)
if sum(a) < m:
	print(0)
elif sum(b) > m:
	print(-1)
else:
	temp = sum(a) - m
	c.sort(reverse=True)
	count = 0
	while temp > 0:
		temp = temp - c[count]
		count += 1
	print(count)


f = lambda : map(int, input().split())
(n, m) = f()
(s, c, ans) = (0, [], 0)
for i in range(n):
	(a, b) = f()
	s += a
	c.append(a - b)
c.sort()
for i in range(n - 1, -1, -1):
	if s <= m:
		break
	s -= c[i]
	ans += 1
if s > m:
	print(-1)
else:
	print(ans)


(n, m) = map(int, input().split())
s = []
comp = 0
total = 0
totalcomp = 0
for i in range(n):
	(a, b) = map(int, input().split())
	s.append(a - b)
	total += a
	totalcomp += a - b
if m < total - totalcomp:
	print('-1')
	exit()
s = sorted(s, reverse=True)
k = 0
while m < total:
	comp += 1
	total -= s[k]
	k += 1
print(comp)


def start():
	(n, m) = map(int, input().split())
	songs = []
	for i in range(n):
		songs.append(list(map(int, input().split())))
	if sum(map(lambda song: song[1], songs)) > m:
		print(-1)
		return
	songs.sort(key=lambda song: song[0] - song[1], reverse=True)
	space = sum(map(lambda song: song[0], songs))
	ans = 0
	while space > m and ans < n:
		space -= songs[ans][0] - songs[ans][1]
		ans += 1
	print(ans)
start()


import sys
(n, m) = map(int, sys.stdin.readline().split())
song = [None] * n
somme = 0
for loop in range(n):
	(d, f) = map(int, sys.stdin.readline().split())
	somme += d
	song[loop] = d - f
song.sort(reverse=True)
total = 0
while total < n and somme > m:
	somme -= song[total]
	total += 1
if somme > m:
	print(-1)
else:
	print(total)


def f(w):
	return w
(n, m) = map(int, input().split())
a = []
b = []
c = []
for i in range(n):
	(a1, b1) = map(int, input().split())
	a.append(a1)
	b.append(b1)
	c.append(a1 - b1)
if sum(a) <= m:
	print(0)
	exit()
if sum(b) > m:
	print(-1)
	exit()
c.sort()
res = sum(a)
for i in range(n - 1, -1, -1):
	res -= c[i]
	if res <= m:
		print(n - i)
		break


(n, m) = map(int, input().split())
vol = 0
arr = []
for i in range(n):
	(a, b) = map(int, input().split())
	vol += a
	arr.append(b - a)
arr.sort()
for i in range(n):
	if vol <= m:
		print(i)
		exit()
	vol += arr[i]
if vol > m:
	print(-1)
else:
	print(n)


def main():
	(n, m) = map(int, input().split())
	songs = []
	total = 0
	for i in range(n):
		(u, v) = map(int, input().split())
		songs.append((u, v, u - v))
		total += u
	songs = sorted(songs, key=lambda song: song[2], reverse=True)
	for i in range(len(songs)):
		if total <= m:
			print(i)
			return
		total -= songs[i][2]
	if total <= m:
		print(n)
	else:
		print(-1)
main()


(n, m) = map(int, input().split())
arr = []
(usum, csum) = (0, 0)
for i in range(n):
	(u, c) = map(int, input().split())
	usum += u
	csum += c
	arr.append(u - c)
if usum <= m:
	print(0)
elif csum > m:
	print(-1)
else:
	arr.sort()
	ans = 0
	for x in reversed(arr):
		usum -= x
		ans += 1
		if usum <= m:
			break
	print(ans)


(n, m) = map(int, input().split())
s = [[0] * 3 for i in range(n)]
for i in range(n):
	(l, r) = map(int, input().split())
	s[i][0] = l - r
	s[i][1] = l
	s[i][2] = r
kl = 0
k = 0
s = sorted(s, reverse=True)
for i in range(0, n):
	k += s[i][1]
for i in range(0, len(s)):
	if k <= m:
		break
	kl += 1
	k -= s[i][0]
if k <= m:
	print(kl)
else:
	print('-1')


(n, m) = map(int, input().split())
c = []
sum1 = 0
sum2 = 0
for i in range(n):
	(a, b) = map(int, input().split())
	sum1 = sum1 + a
	sum2 = sum2 + b
	c.append(a - b)
if sum2 > m:
	print(-1)
else:
	c.sort()
	count = 0
	while sum1 > m:
		sum1 = sum1 - c[-1]
		count = count + 1
		c.pop()
	print(count)


(n, m) = [int(x) for x in input().split()]
compressdiff = list()
initsum = 0
compresssum = 0
for i in range(n):
	(a, b) = [int(x) for x in input().split()]
	compressdiff.append(a - b)
	initsum += a
	compresssum += b
if initsum <= m:
	print('0')
elif compresssum > m:
	print('-1')
else:
	compressdiff.sort(reverse=True)
	need = initsum - m
	count = 0
	i = 0
	while need > 0:
		count += 1
		need = need - compressdiff[i]
		i += 1
	print(count)


def songs(n, m, a):
	s1 = 0
	s2 = 0
	s = 1
	b = []
	for i in range(n):
		s1 += a[i][0]
		s2 += a[i][1]
		b.append(a[i][0] - a[i][1])
	if m < s2:
		return -1
	elif m >= s1:
		return 0
	else:
		m = s1 - m
		b.sort()
		for j in range(len(b)):
			if m - b[-j - 1] > 0:
				s += 1
				m = m - b[-j - 1]
			else:
				return s
(n, m) = list(map(int, input().split()))
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
print(songs(n, m, a))


(n, m) = map(int, input().split())
sc = []
for _ in range(n):
	(a, b) = map(int, input().split())
	m -= b
	sc.append(a - b)
for s in sorted(sc):
	if m >= s:
		m -= s
		n -= 1
print(-1 if m < 0 else n)


(n, m) = map(int, input().split())
s = []
size = 0
for _ in range(n):
	s.append(list(map(int, input().split())))
	size += s[-1][0]
size = size - m
if size <= 0:
	print(0)
else:
	s = [i[0] - i[1] for i in s]
	s.sort(reverse=True)
	count = 0
	for i in range(n):
		size -= s[i]
		count += 1
		if size <= 0:
			print(count)
			break
	else:
		print(-1)


nm = input().split()
(n, m) = (int(nm[0]), int(nm[1]))
a = []
b = []
for i in range(n):
	ab = input().split()
	a.append(int(ab[0]))
	b.append(int(ab[1]))
if sum(b) > m:
	print('-1')
elif sum(a) <= m:
	print('0')
elif sum(b) == m:
	print(n)
else:
	x = []
	for i in range(n):
		x.append((a[i] - b[i], a[i]))
	c = 0
	x.sort()
	y = sum(a) - m
	while y > 0:
		y -= x[len(x) - 1 - c][0]
		c += 1
	print(c)


def main():
	(n, m) = map(int, input().split())
	meta = []
	for i in range(n):
		(a, b) = map(int, input().split())
		meta.append((a - b, a))
	meta.sort(reverse=True, key=lambda x: x[0])
	sm = 0
	for (a, b) in meta:
		sm += b
	cnt = 0
	idx = 0
	while idx < n and sm > m:
		cnt += 1
		sm -= meta[idx][0]
		idx += 1
	if sm <= m:
		print(cnt)
	else:
		print(-1)
main()


(n, k) = map(int, input().split())
(l, d) = ([], [])
s = 0
for i in range(n):
	ll = list(map(int, input().split()))
	l.append(ll[0] - ll[1])
	s = s + ll[0]
c = 0
l.sort(reverse=True)
while s > k:
	if c == n:
		print(-1)
		exit()
	s = s - l[c]
	c = c + 1
print(c)


def mi():
	return map(int, input().split())
(n, m) = mi()
diff = [0] * n
s1 = 0
s2 = 0
for i in range(n):
	(n1, n2) = mi()
	s1 += n1
	s2 += n2
	diff[i] = n1 - n2
diff.sort()
if s2 > m:
	print(-1)
else:
	diff.sort(reverse=True)
	c = 0
	for i in diff:
		if s1 <= m:
			break
		s1 -= i
		c += 1
	print(c)


(n, m) = map(int, input().split())
f = []
u = 0
for j in range(n):
	v = list(map(int, input().split()))
	f.append(v[0] - v[1])
	u += v[0]
f.sort()
if u <= m:
	print(0)
else:
	b = 0
	h = 0
	for j in range(n - 1, -1, -1):
		u -= f[j]
		b += 1
		if u <= m:
			print(b)
			h += 1
			break
	if h == 0:
		print(-1)


(n, m) = map(int, input().split())
array = []
s = 0
count = 0
for i in range(n):
	(a, b) = map(int, input().split())
	s += a
	array.append(a - b)
array.sort(reverse=True)
i = 0
while s > m:
	if i == n:
		print(-1)
		exit(0)
	d = array[i]
	s -= d
	count += 1
	i += 1
print(count)


(n, m) = map(int, input().split())
summ = 0
z = []
for i in range(n):
	(a, b) = map(int, input().split())
	z += [a - b]
	summ += a
i = -1
z.sort(reverse=True)
while m < summ and i <= len(z) - 2:
	i += 1
	summ -= z[i]
if summ > m:
	print(-1)
else:
	print(i + 1)


(n, m) = map(int, input().split())
C = []
S = 0
k = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	S += a
	C.append(a - b)
if S > m + sum(C):
	print(-1)
elif S <= m:
	print(0)
else:
	C.sort(reverse=True)
	ind = 0
	while S > m:
		S = S - C[ind]
		ind += 1
	print(ind)


(n, m) = map(int, input().split())
a = []
b = []
diff = []
for _ in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	b.append(y)
suma = sum(a)
sumb = sum(b)
if suma <= m:
	print(0)
	exit()
if sumb > m:
	print(-1)
	exit()
for i in range(0, n):
	diff.append(a[i] - b[i])
diff.sort()
diff.reverse()
count = 1
compress = suma - m
for i in range(0, n):
	temp = compress - diff[i]
	if temp > 0:
		count = count + 1
		compress = compress - diff[i]
	else:
		break
print(count)


a = [int(x) for x in input().split()]
sum = 0
diff = list()
for x in range(a[0]):
	b = [int(x) for x in input().split()]
	sum += b[0]
	diff.append(b[0] - b[1])
diff.sort()
length = len(diff)
count = 0
while count < length:
	if sum <= a[1]:
		print(count)
		break
	sum -= diff[length - count - 1]
	count += 1
else:
	if sum <= a[1]:
		print(count)
	else:
		print(-1)


I = lambda : map(int, input().split())
(n, m) = I()
a = []
(sx, sy) = (0, 0)
for i in range(n):
	(x, y) = I()
	a.append(x - y)
	sx += x
	sy += y
a.sort(reverse=True)
if sy > m:
	exit(print(-1))
elif sx <= m:
	print(0)
else:
	i = 0
	while sx > m:
		sx = sx - a[i]
		i += 1
	print(i)


(n, m) = map(int, input().split())
k = 0
arr = [[int(i) for i in input().split()] for _ in range(n)]
s = sum((i[0] for i in arr))
for j in sorted([i[0] - i[1] for i in arr], reverse=True):
	if s <= m:
		print(k)
		exit()
	s -= j
	k += 1
if s <= m:
	print(k)
	exit()
print(-1)


(N, M) = map(int, input().split())
d = []
for a in range(N):
	(A, B) = map(int, input().split())
	d.append((A, B, A - B))
V = sum([x[1] for x in d])
K = sum([x[0] for x in d])
if V > M:
	print(-1)
elif K <= M:
	print(0)
else:
	S = K - M
	L = sorted(d, key=lambda x: x[2], reverse=True)
	c = 0
	while S > 0:
		S -= L[c][2]
		c = c + 1
	print(c)


(n, m) = map(int, input().split())
a = []
b = 0
for i in range(n):
	a.append(list(map(int, input().split())))
	a[i].append(a[i][0] - a[i][1])
	(a[i][0], a[i][1], a[i][2]) = (a[i][2], a[i][0], a[i][1])
a.sort()
summ = 0
for j in range(n):
	summ = summ + a[j][1]
for k in range(-1, -n - 1, -1):
	if m < summ:
		summ = summ - a[k][0]
		b += 1
	else:
		break
if m < summ:
	print('-1')
else:
	print(b)


(n, k) = list(map(int, input().split()))
l = 0
(a, b) = ([], [])
for i in range(n):
	(l, r) = list(map(int, input().split()))
	a.append(l)
	b.append(r)
l = sum(a)
if l <= k:
	print(0)
else:
	c = []
	for i in range(n):
		c.append(a[i] - b[i])
	c.sort()
	c.reverse()
	i = 0
	while i < n and l > k:
		l = l - c[i]
		i += 1
	if l > k:
		print(-1)
	else:
		print(i)


(n, m) = map(int, input().split())
a = []
b = []
c = []
for i in range(n):
	(ai, bi) = map(int, input().split())
	a.append(ai)
	b.append(bi)
	c.append(ai - bi)
if sum(b) > m:
	print(-1)
elif sum(a) <= m:
	print(0)
else:
	x = max(0, abs(m - sum(a)))
	c.sort(reverse=1)
	ans = 0
	i = 0
	while x > 0 and i < n:
		x -= c[i]
		ans += 1
		i += 1
	print(ans)


(n, m) = map(int, input().split())
a = []
sum = 0
for i in range(n):
	(x, y) = map(int, input().split())
	sum += x
	a.append(x - y)
b = sorted(a, reverse=True)
ans = 0
k = 0
if sum <= m:
	print(ans)
else:
	for i in range(len(b)):
		sum -= b[i]
		ans += 1
		if sum <= m:
			print(ans)
			break
	else:
		print(-1)


from queue import PriorityQueue as pq

def create_songs(songs_count):
	songs = pq()
	uncompressed_size_total = 0
	compressed_size_total = 0
	for i in range(songs_count):
		(uncompressed_size, compressed_size) = map(int, input().split())
		uncompressed_size_total += uncompressed_size
		compressed_size_total += compressed_size
		songs.put((-(uncompressed_size - compressed_size), uncompressed_size, compressed_size))
	return (songs, uncompressed_size_total, compressed_size_total)

def zip_song(songs, space_usb, space_needed):
	zipped_songs_count = 0
	while not songs.empty() and space_usb < space_needed:
		song = songs.get()
		space_usb -= song[2]
		space_needed -= song[1]
		zipped_songs_count += 1
	return zipped_songs_count

def solve(songs_count, space_usb):
	(songs, uncompressed_size_total, compressed_size_total) = create_songs(songs_count)
	if uncompressed_size_total <= space_usb:
		return 0
	elif compressed_size_total > space_usb:
		return -1
	else:
		return zip_song(songs, space_usb, uncompressed_size_total)

def solve2(songs_count, space_usb):
	songs = []
	unzipped_total = 0
	zipped_total = 0
	for i in range(songs_count):
		(unzipped, zipped) = map(int, input().split())
		unzipped_total += unzipped
		zipped_total += zipped
		songs.append((unzipped - zipped, unzipped, zipped))
	if unzipped_total < space_usb:
		return 0
	elif zipped_total > space_usb:
		return -1
	space_songs = unzipped_total
	songs.sort()
	count = 0
	while space_songs > space_usb:
		song = songs.pop()
		space_songs -= song[1]
		space_usb -= song[2]
		count += 1
	return count
(songs_count, space_usb) = map(int, input().split())
result = solve2(songs_count, space_usb)
print(result)


(n, m) = map(int, input().split())
a = []
s = 0
S = 0
for i in range(n):
	(x, y) = map(int, input().split())
	s += x
	S += y
	a.append([x - y, x, y])
a = sorted(a)[::-1]
if S > m:
	print(-1)
else:
	i = 0
	ans = 0
	while s > m:
		s -= a[i][0]
		ans += 1
		i += 1
	print(ans)


(n, m) = list(map(int, input().split(' ')))
l = []
s = 0
for i in range(n):
	(ai, bi) = list(map(int, input().split(' ')))
	l.append((ai - bi, ai, bi))
	s += ai
l.sort(reverse=True)
c = 0
for i in range(n):
	if s <= m:
		break
	else:
		s -= l[i][0]
		c += 1
if s <= m:
	print(c)
else:
	print(-1)


(n, m) = map(int, input().split())
arr = []
diff = []
add = 0
c = 0
for i in range(n):
	arr.append(list(map(int, input().split())))
a = list(zip(*arr))
for (j, k) in arr:
	diff.append(j - k)
diff.sort(reverse=True)
k = sum(a[0]) - m
if k > sum(diff):
	print('-1')
else:
	for i in diff:
		if add < k and k > 0:
			add = add + i
			c += 1
	print(c)


(n, m) = map(int, input().split())
l = []
s = 0
for i in range(n):
	(a, b) = map(int, input().split())
	s = s + a
	l.append(a - b)
l.sort(reverse=True)
if s <= m:
	print(0)
else:
	f = 0
	c = 0
	for i in l:
		s = s - i
		c = c + 1
		if s <= m:
			f = 1
			break
	if f:
		print(c)
	else:
		print(-1)


(n, m) = map(int, input().split())
(a, b, dif) = ([], [], [])
for i in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	b.append(y)
	dif.append(x - y)
if sum(b) > m:
	print(-1)
elif sum(a) <= m:
	print(0)
else:
	cur = sum(a)
	ans = 0
	for i in sorted(dif, reverse=True):
		if cur <= m:
			break
		cur = cur - i
		ans += 1
	print(ans)


first_line = [int(x) for x in input().split()]
n = first_line[0]
m = first_line[1]
a = []
b = []
for x in range(n):
	line = [int(x) for x in input().split()]
	a.append(line[0])
	b.append(line[1])
if sum(b) > m:
	print(-1)
else:
	for i in range(n):
		b[i] = a[i] - b[i]
	c = sorted(b, reverse=True)
	result = 0
	_sum = sum(a)
	i = 0
	while _sum > m:
		_sum -= c[i]
		i += 1
		result += 1
	print(result)


(n, m) = map(int, input().split())
a = []
(total_sum, cnt, j, flag) = (0, 0, 0, 0)
for i in range(n):
	x = list(map(int, input().split()))
	a.append(x[0] - x[1])
	total_sum += x[0]
a.sort(reverse=True)
while total_sum > m:
	try:
		total_sum -= a[j]
		cnt += 1
		j += 1
	except IndexError:
		flag = 1
		print(-1)
		break
if flag == 0:
	print(cnt)


(nbMusiques, taille) = map(int, input().split())
tableau = []
somme = 0
for i in range(nbMusiques):
	(avant, apres) = map(int, input().split())
	somme += avant
	tableau.append(avant - apres)
tableau.sort()
if somme <= taille:
	print(0)
else:
	for i in range(nbMusiques):
		somme -= tableau[nbMusiques - 1 - i]
		if somme <= taille:
			print(i + 1)
			break
	if somme > taille:
		print(-1)


(n, m) = map(int, input().split())
mas = []
k = 0
for i in range(n):
	a = [int(s) for s in input().split()]
	mas.append(a[0] - a[1])
	k += a[0]
mas = sorted(mas, reverse=True)
i = 0
while k > m and i < n:
	k -= mas[i]
	i += 1
if i == n and k > m:
	print(-1)
	exit()
print(i)


(n, m) = map(int, input().split())
dif = []
foo = 0
cnt = 0
for i in range(n):
	(a, b) = map(int, input().split())
	dif.append(a - b)
	foo += a
dif.sort()
dif.reverse()
if foo <= m:
	print(0)
else:
	for i in dif:
		foo -= i
		cnt += 1
		if foo <= m:
			print(cnt)
			exit()
	print(-1)


import os
import sys
from io import BytesIO, IOBase
import math
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(n, m) = map(int, input().split())
ai = []
bi = []
for i in range(n):
	(l, r) = map(int, input().split())
	ai.append(l)
	bi.append(l - r)
sum = 0
ss = 0
for i in range(n):
	sum += ai[i]
	ss += bi[i]
bi.sort(reverse=True)
if sum - ss > m:
	print('-1')
elif sum < m:
	print('0')
else:
	j = 0
	c = 0
	while sum > m:
		sum -= bi[c]
		c += 1
		j += 1
	print(c)


(n, m) = map(int, input().split())
arr = []
before_sum = 0
diff_arr = []
comp_sum = 0
for i in range(n):
	(x, y) = map(int, input().split())
	before_sum += x
	comp_sum += y
	diff = x - y
	diff_arr.append(diff)
if comp_sum > m:
	print(-1)
else:
	if before_sum < m:
		print(0)
		quit()
	diff_arr.sort()
	count = 0
	while comp_sum < m:
		a = diff_arr.pop(0)
		comp_sum += a
		if comp_sum > m:
			break
		else:
			count += 1
	print(n - count)


(n, m) = [int(x) for x in input().split()]
a = [0] * n
diff = [0] * n
for i in range(n):
	(a[i], diff[i]) = [int(x) for x in input().split()]
	diff[i] = a[i] - diff[i]
s = m - sum(a)
diff.sort()
net = 0
for d in diff[-1::-1]:
	if s >= 0:
		break
	s += d
	net += 1
print(net if s >= 0 else -1)


cnt = lambda s, x: s.count(x)
ii = lambda : int(input())
si = lambda : input()
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
dgl = lambda : list(map(int, input()))
(n, sz) = f()
sm = 0
l = []
for _ in range(n):
	(a, b) = f()
	sm += a
	l.append(a - b)
k = sm - sz
l.sort(reverse=True)
i = 0
while k > 0 and i < n:
	k -= l[i]
	i += 1
print(i if k <= 0 else -1)


(n, m) = map(int, input().split())
a = [0] * n
s = 0
for i in range(n):
	(d, c) = map(int, input().split())
	a[i] = d - c
	s = s + d
a.sort(reverse=True)
i = 0
m1 = s - m
while m1 > 0:
	m1 -= a[i]
	i += 1
	if i == n:
		break
if m1 > 0:
	print(-1)
else:
	print(i)


nm = list(map(int, input().split()))
n = nm[0]
m = nm[1]
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
maxsum = 0
diffs = []
for i in range(n):
	maxsum += a[i][0]
	diffs.append(a[i][0] - a[i][1])
diffs = sorted(diffs)
diffs.reverse()
if maxsum <= m:
	print(0)
	exit()
cur = 0
while maxsum > m:
	if cur == len(diffs):
		print(-1)
		exit()
	maxsum -= diffs[cur]
	cur += 1
print(cur)


(n, m) = map(int, input().split())
L = [0] * n
V = 0
p = 0
A = [0] * n
M = []
for i in range(n):
	(a, b) = map(int, input().split())
	V += a
	M.append((a - b, a, b))
M.sort(reverse=True)
cnt = 0
next_ = V
for i in range(n):
	if cnt + V <= m:
		print(i)
		break
	cnt += M[i][2]
	V -= M[i][1]
else:
	if cnt + V <= m:
		print(n)
	else:
		print(-1)


def songsCompression(songs, n, capacity, sum):
	songs.sort(key=lambda tup: tup[0] - tup[1], reverse=True)
	compressed = 0
	if sum <= capacity:
		return 0
	for song in songs:
		if sum - (song[0] - song[1]) > capacity:
			sum -= song[0] - song[1]
			compressed += 1
		else:
			compressed += 1
			return compressed
	return -1
(n, capacity) = input().split()
songs = []
sum = 0
for i in range(int(n)):
	line = input().split()
	sum += int(line[0])
	songs.append((int(line[0]), int(line[1])))
print(songsCompression(songs, int(n), int(capacity), sum))


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
Read = lambda : list(map(int, input().split()))
Num = lambda : int(input())

def solve():
	(n, m) = Read()
	(a, b) = ([], [])
	(sa, sb) = (0, 0)
	diff = []
	for _ in range(n):
		(x, y) = Read()
		sa += x
		sb += y
		diff.append(x - y)
		a.append(x)
		b.append(y)
	extra = sa - m
	if sb > m:
		print(-1)
		return
	if extra <= 0:
		print(0)
	else:
		diff.sort(reverse=1)
		ch = 0
		sd = 0
		for i in range(len(diff)):
			sd += diff[i]
			ch += 1
			if sd >= extra:
				break
		print(ch)
solve()


l = input()
(n, m) = map(lambda el: int(el), l.split())
songs = []
space = 0
for i in range(0, n):
	l = input()
	(u, c) = map(lambda el: int(el), l.split())
	space += u
	songs.append((u, c))
if space > m:
	songs.sort(key=lambda el: el[0] - el[1], reverse=True)
	c_count = 0
	for song in songs:
		space -= song[0] - song[1]
		c_count += 1
		if space <= m:
			print(c_count)
			break
	if space > m:
		print('-1')
else:
	print('0')


Input = lambda : map(int, input().split())
(n, m) = Input()
dx = []
sum = 0
for i in range(n):
	(a, b) = Input()
	sum += a
	dx.append(a - b)
ans = 0
for x in sorted(dx, reverse=True):
	if sum <= m:
		break
	sum -= x
	ans += 1
if sum <= m:
	print(ans)
else:
	print(-1)


(n, m) = map(int, input().split())
diffLs = []
currentUsage = 0
for i in range(n):
	(a, b) = map(int, input().split())
	diffLs.append(abs(a - b))
	currentUsage += a
diffLs.sort()
diffLs = diffLs[::-1]
count = 0
while currentUsage > m:
	if len(diffLs) == 0:
		count = -1
		break
	else:
		count += 1
		currentUsage -= diffLs.pop(0)
print(count)


from heapq import heappush, heappop
(n, m) = map(int, input().split(' '))
(sa, sb, diffs) = (0, 0, [])
for i in range(n):
	(a, b) = tuple(map(int, input().split(' ')))
	sa += a
	sb += b
	heappush(diffs, b - a)
if m < sb:
	print(-1)
elif m > sa:
	print(0)
else:
	res = 0
	while sa > m:
		sa += heappop(diffs)
		res += 1
	print(res)


(n, m) = map(int, input().split())
over = 0 - m
a = []
b = []
d = []
for i in range(0, n):
	(ai, bi) = map(int, input().split())
	a.append(ai)
	b.append(bi)
	d.append(ai - bi)
	over += ai
d.sort(reverse=True)
c = 0
while over > 0 and c < n:
	over -= d[c]
	c = c + 1
if c >= n and over > 0:
	print(-1)
else:
	print(c)


(n, m) = [int(a) for a in input().split()]
data1 = []
data2 = []
size1 = 0
size2 = 0
for i in range(n):
	data1.append([int(b) for b in input().split()])
for i in range(n):
	size1 += data1[i][1]
if size1 > m:
	print(-1)
else:
	for i in range(n):
		size2 += data1[i][0]
	if size2 <= m:
		print(0)
	else:
		for i in range(n):
			data2.append(data1[i][0] - data1[i][1])
		data2 = sorted(data2, reverse=True)
		for i in range(n):
			size2 -= data2[i]
			if size2 <= m:
				print(i + 1)
				break
			else:
				continue


(n, m) = map(int, input().split())
total_size = 0
total_compar_size = 0
list_differ = []
for i in range(n):
	(size, compar_size) = map(int, input().split())
	total_size += size
	total_compar_size += compar_size
	list_differ.append(size - compar_size)
if m < total_compar_size:
	print('-1')
elif m >= total_size:
	print('0')
else:
	list_differ.sort()
	count = 0
	for i in range(1, n + 1):
		total_size -= list_differ[-i]
		count += 1
		if total_size <= m:
			print(count)
			break


(n, vol) = map(int, input().split())
d = []
for i in range(n):
	(a, b) = map(int, input().split())
	d.append((a, b, a - b))
if sum(map(lambda x: x[0], d)) <= vol:
	print(0)
elif sum(map(lambda x: x[1], d)) > vol:
	print(-1)
else:
	count = 1
	d.sort(key=lambda x: x[2], reverse=True)
	summa = d[0][1]
	other = sum(map(lambda x: x[0], d[1:]))
	while summa + other > vol:
		summa += d[count][1]
		other -= d[count][0]
		count += 1
	print(count)


(n, m) = map(int, input().split())
a = []
b = []
d = []
for i in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	b.append(y)
	d.append(x - y)
l = 0
d = sorted(d)
d.reverse()
if sum(b) > m:
	print(-1)
elif sum(b) == m:
	print(n)
else:
	k = sum(a)
	z = k - m
	for i in range(len(d)):
		if z <= 0:
			break
		z -= d[i]
		l += 1
	print(l)


(n, s) = map(int, input().split())
arr = []
total = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	total += a
	arr.append(a - b)
arr.sort()
arr.reverse()
i = 0
while total > s:
	if i == n:
		print(-1)
		exit()
	total -= arr[i]
	i += 1
print(i)


(n, size) = map(int, input().split())
pot = list()
tot = 0
for i in range(n):
	(a, b) = map(int, input().split())
	pot.append(a - b)
	tot += a
pot.sort()
pot = pot[::-1]
cpt = 0
while tot > size and cpt < len(pot):
	tot -= pot[cpt]
	cpt += 1
if tot > size:
	print(-1)
else:
	print(cpt)


(n, m) = map(int, input().split())
s = []
s1 = []
for i in range(n):
	(a, b) = map(int, input().split())
	s.append(a)
	s1.append(a - b)
x = sum(s)
s1.sort()
ans = 0
while x > m and len(s1) > 0:
	ans += 1
	x -= s1[-1]
	s1.pop()
if x > m:
	print(-1)
else:
	print(ans)


(n, m) = map(int, input().split())
ab = [[int(x) for x in input().split()] for _ in range(n)]

def inv(x):
	return 10 ** 9 - x
import heapq
ans = 0
c = m
q = []
for (a, b) in ab:
	m -= a
	heapq.heappush(q, inv(a - b))
while m < 0 and q:
	m += inv(heapq.heappop(q))
	ans += 1
if m < 0:
	print(-1)
else:
	print(ans)


(n, m) = tuple(map(int, input().split()))
total = 0
compression = []
for i in range(n):
	(a, b) = tuple(map(int, input().split()))
	total += a
	compression.append(a - b)
compression.sort(reverse=True)
j = 0
while total > m and j < n:
	total -= compression[j]
	j += 1
if total <= m:
	print(j)
else:
	print(-1)


import sys
from functools import reduce
input = sys.stdin.readline

def readPair():
	return tuple(map(int, input().split()))

def readEntry():
	e = readPair()
	return (e[0], e[1], e[0] - e[1])
(n, m) = readPair()
entries = [readEntry() for _ in range(0, n)]
entries.sort(key=lambda x: x[2], reverse=True)
size = reduce(lambda s, e: s + e[0], entries, 0)
count = 0
while size > m and count < n:
	size -= entries[count][2]
	count += 1
print(-1 if size > m else count)


(n, m) = map(int, input().split())
l = []
p = []
minus = []
for _ in range(n):
	(a, b) = map(int, input().split())
	l.append(a)
	p.append(b)
	minus.append(a - b)
sum_p = sum(p)
minus.sort(reverse=True)
if sum_p > m:
	print(-1)
elif sum_p == m:
	print(n)
else:
	c = 0
	i = 0
	sum_l = sum(l)
	flag = 0
	if sum_l <= m:
		print(0)
	else:
		for i in range(0, n):
			if sum_l - minus[i] <= m:
				print(i + 1)
				flag = 1
				break
			sum_l -= minus[i]
		if flag == 0:
			print(n)


import re
in1 = [int(x) for x in re.split('\\s', input())]
space = 0
diff = [0] * in1[0]
for x in range(in1[0]):
	inX = [int(x) for x in re.split('\\s', input())]
	diff[x] = inX[0] - inX[1]
	space += inX[0]
diff.sort()
count = 0
x = -1
while space > in1[1] and x > -len(diff):
	space -= diff[x]
	count += 1
	x -= 1
if space - diff[0] > in1[1]:
	print(-1)
elif space > in1[1]:
	print(count + 1)
else:
	print(count)


(n, m) = map(int, input().split())
tot = 0
diff = []
for i in range(n):
	(a, b) = map(int, input().split())
	tot += a
	diff.append(a - b)
t = tot - m
diff.sort(reverse=True)
Ans = 0
while t > 0 and Ans < n:
	t -= diff[Ans]
	Ans += 1
print(Ans) if t <= 0 else print(-1)


(n, m) = map(int, input().split())
s = []
uc = 0
c = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	s.append((a, b))
	uc += a
t = sorted(s, key=lambda x: x[0] - x[1], reverse=True)
for i in range(n):
	if uc <= m:
		break
	uc -= t[i][0] - t[i][1]
	c += 1
if uc > m:
	print(-1)
else:
	print(c)


import sys
from collections import deque, Counter
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))
(n, m) = inpl()
su = 0
sa = []
for _ in range(n):
	(a, b) = inpl()
	sa.append(a - b)
	su += a
sa.sort(reverse=True)
res = 0
for (i, x) in enumerate(sa):
	if su <= m:
		print(res)
		quit()
	su -= x
	res += 1
if su <= m:
	print(res)
else:
	print(-1)


(n, m) = map(int, input().split())
s = 0
h = []
while n != 0:
	(a, b) = map(int, input().split())
	h += [a - b]
	s += a
	n -= 1
ans = 0
h.sort()
j = len(h) - 1
while j >= 0 and s > m:
	s -= h[j]
	ans += 1
	j -= 1
if s > m:
	print(-1)
else:
	print(ans)


(n, m) = map(int, input().split())
a = []
for i in range(n):
	(x, y) = map(int, input().split())
	a.append(x - y)
	m -= y
cnt = n
a = sorted(a)
for i in range(n):
	if a[i] <= m:
		m -= a[i]
		cnt -= 1
if m < 0:
	cnt = -1
print(cnt)


s = list(map(int, input().split()))
(n, m) = (s[0], s[1])
a = []
b = []
c = []
for i in range(n):
	s = list(map(int, input().split()))
	a.append(s[0])
	b.append(s[1])
	c.append(s[0] - s[1])
if sum(b) > m:
	print(-1)
else:
	counter = 0
	pos = n - 1
	c = sorted(c)
	sm = sum(a)
	while sm > m:
		sm -= c[pos]
		pos -= 1
		counter += 1
	print(counter)


def find_min_compressions(songs, n, m):
	size = sum([a for (a, _) in songs])
	songs.sort(key=lambda x: x[0] - x[1], reverse=True)
	total = 0
	i = 0
	while i < n and size > m:
		total += 1
		(a, b) = songs[i]
		size -= a - b
		i += 1
	return total if size <= m else -1
(n, m) = map(int, input().strip().split())
songs = []
for _ in range(n):
	(a, b) = map(int, input().strip().split())
	songs.append((a, b))
print(find_min_compressions(songs, n, m))


(n, m) = map(int, input().split())
pres = []
comps = []
summ = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	comps.append([a - b, b, a])
	summ += a
comps.sort(key=lambda x: x[0], reverse=True)
if summ <= m:
	print(0)
else:
	eles = 0
	for i in range(n):
		summ -= comps[i][0]
		eles += 1
		if summ <= m:
			break
	if summ > m:
		print(-1)
	else:
		print(eles)


(n, m) = [int(i) for i in input().split()]
a = []
b = []
c = []
for i in range(n):
	(u, v) = [int(j) for j in input().split()]
	c.append(u - v)
	a.append(u)
	b.append(v)
s = sum(a) - m
c = list(reversed(sorted(c)))
r = 0
ans = -1
if r >= s:
	ans = 0
if ans == -1:
	for i in range(n):
		r += c[i]
		if r >= s:
			ans = i + 1
			break
print(ans)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
import random
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def main():
	(n, m) = LI()
	aa = [LI() for _ in range(n)]
	s = sum([x[0] for x in aa])
	if s <= m:
		return 0
	a = sorted([x[1] - x[0] for x in aa])
	for i in range(n):
		s += a[i]
		if s <= m:
			return i + 1
	return -1
print(main())


(n, m) = map(int, input().split())
x = []
summ = 0
for i in range(n):
	(a, b) = map(int, input().split())
	summ += a
	x.append([a, b])

def f(m):
	return m[0] - m[1]
x.sort(key=f, reverse=True)
count = 0
ind = False
while count <= n:
	if summ <= m:
		print(count)
		ind = True
		break
	if count == n:
		break
	summ -= x[count][0]
	summ += x[count][1]
	count += 1
if not ind:
	print(-1)


(n, m) = [int(i) for i in input().split(' ')]
s = 0
d = []
for i in range(n):
	(a, b) = [int(i) for i in input().split(' ')]
	s += a
	d.append(a - b)
d.sort(reverse=1)
ans = 0
if s <= m:
	print(0)
elif s - sum(d) > m:
	print(-1)
else:
	for i in d:
		s -= i
		ans += 1
		if s <= m:
			break
	print(ans)


hello = input().split(' ')
vok = {}
arr = []
for i in range(int(hello[0])):
	hello1 = input()
	vok[i] = hello1.split(' ')
	arr.append(int(vok[i][0]) - int(vok[i][1]))
first_sum = 0
last_sum = 0
for key in vok.keys():
	first_sum += int(vok[key][0])
	last_sum += int(vok[key][1])
differ = first_sum - last_sum
if last_sum > int(hello[1]):
	print('-1')
	exit()
if first_sum <= int(hello[1]):
	print('0')
	exit()
how_m = 0
how_m_sum = 0
if differ < first_sum - int(hello[1]):
	print('-1')
	exit()
else:
	for el in sorted(arr, reverse=True):
		if how_m_sum + int(el) < first_sum - int(hello[1]):
			how_m += 1
			how_m_sum += el
		else:
			how_m += 1
			print(how_m)
			exit()


(n, m) = map(int, input().split())
l = []
for i in range(n):
	(a, b) = map(int, input().split())
	l.append([a, b])
z = []
sum = 0
sum2 = 0
for i in range(n):
	sum += l[i][0]
	sum2 += l[i][1]
	z.append(l[i][0] - l[i][1])
z.sort(reverse=True)
if sum2 > m:
	print(-1)
else:
	j = 0
	while sum > m:
		sum -= z[j]
		j += 1
	print(j)


(n, m) = map(int, input().split(' '))
ta = 0
ecs = []
for i in range(n):
	(a, b) = map(int, input().split(' '))
	ta += a
	ecs += [a - b]
ecs = sorted(ecs, reverse=True)
j = 0
res = 0
while ta > m and j < len(ecs):
	ta -= ecs[j]
	res += 1
	j += 1
print(-1 if ta > m else res)


(n, m) = map(int, input().split())
d = []
dt = 0
s = 0
p = 0
c = 0
for i in range(n):
	(l, r) = map(int, input().split())
	d.append(l - r)
	dt += d[i]
	s += l
	p += r
if p > m:
	print(-1)
	print()
else:
	d.sort(reverse=True)
	t = s
	for i in d:
		if t <= m:
			break
		else:
			t -= i
			c += 1
	print(c)


(a, b) = map(int, input().split())
f = []
s = 0
for _ in ' ' * a:
	(c, d) = map(int, input().split())
	b -= d
	f += [c - d]
for _ in sorted(f):
	if b >= _:
		b -= _
		a -= 1
print([a, -1][b < 0])


import sys
input = sys.stdin.readline

def readPair():
	return tuple(map(int, input().split()))
(n, m) = readPair()
entries = [readPair() for _ in range(0, n)]
entries.sort(key=lambda x: x[1] - x[0])
size = sum((x[0] for x in entries))
count = 0
while size > m and count < n:
	size -= entries[count][0] - entries[count][1]
	count += 1
print(-1 if size > m else count)


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
a = list()
b = list()
dis = list()
ans = 0
max_a = 0
max_b = 0
for i in range(n):
	raw = list(map(int, input().split()))
	max_a += raw[0]
	max_b += raw[1]
	a.append(raw[0])
	b.append(raw[1])
	dis.append(abs(raw[0] - raw[1]))
if max_b <= m:
	if max_a <= m:
		ans = 0
	else:
		dis.sort()
		while max_a > m:
			max_a -= dis.pop()
			ans += 1
	print(ans)
else:
	print(-1)


def check():
	global n, m, a, b, ratio
	if sum(b) > m:
		print(-1)
		return
	out = 0
	size = sum(a)
	dik = []
	for i in range(n):
		dik.append((i, ratio[i]))
	dik = sorted(dik, key=lambda x: x[1], reverse=1)
	start = 0
	while size > m:
		size -= a[dik[start][0]] - b[dik[start][0]]
		out += 1
		start += 1
	print(out)
	return
[n, m] = [int(i) for i in input().split()]
a = []
b = []
ratio = []
for i in range(n):
	foo = [int(i) for i in input().split()]
	a.append(foo[0])
	b.append(foo[1])
	ratio.append(foo[0] - foo[1])
check()


from math import *
from copy import *
from string import *
from random import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
import math
(n, m) = map(int, input().split(' '))
l = []
total = 0
for i in range(n):
	l1 = list(map(int, input().split(' ')))
	l.append(abs(l1[0] - l1[1]))
	total += l1[0]
l.sort(reverse=True)
c = 0
i = 0
while total > m and i < n:
	total = total - l[i]
	c = c + 1
	i = i + 1
if total > m:
	print(-1)
else:
	print(c)


(n, m) = map(int, input().split())
a = []
s = 0
for i in range(n):
	(k, b) = map(int, input().split())
	s += k
	a.append(k - b)
a.sort(reverse=True)
ans = 0
for i in range(n):
	if s <= m:
		print(ans)
		exit(0)
	s -= a[i]
	ans += 1
if s <= m:
	print(ans)
else:
	print(-1)


(kolvo, memory) = map(int, input().split())
razn = []
s = 0
for i in range(kolvo):
	(ai, bi) = map(int, input().split())
	s += ai
	razn.append(ai - bi)
if s <= memory:
	print(0)
else:
	razn.sort(reverse=True)
	k = 0
	f = True
	for i in range(kolvo):
		s -= razn[i]
		k += 1
		if s <= memory:
			print(k)
			f = False
			break
	if f:
		print(-1)


import heapq
(n, m) = map(int, input().split())
inp = []
b_total = 0
h = []
for _ in range(n):
	(a, b) = map(int, input().split())
	b_total += b
	inp.append((a, b))
	heapq.heappush(h, a - b)
if b_total > m:
	print(-1)
else:
	m -= b_total
	c = len(inp)
	while m > 0 and h:
		x = heapq.heappop(h)
		tm = m - x
		if tm < 0:
			break
		else:
			m = tm
			c -= 1
	print(c)


(n, m) = map(int, input().split())
(a, b, c) = (list(), list(), list())
res = int()
for _ in range(n):
	(a0, b0) = map(int, input().split())
	a.append(a0)
	b.append(b0)
	c.append(a0 - b0)
if sum(b) > m:
	print(-1)
else:
	c.sort(reverse=True)
	s = sum(a) - m
	for i in range(n):
		if s <= 0:
			print(res)
			break
		else:
			s -= c[i]
			res += 1
	else:
		print(res)


(n, m) = input().split()
n = int(n)
m = int(m)
s = 0
z = 0
h = 0
r = []
for i in range(n):
	(a, b) = input().split()
	a = int(a)
	b = int(b)
	s += a
	r.append(a - b)
r.sort()
if s <= m:
	print(0)
	h = 2
if h == 0:
	for i in range(len(r)):
		z += r[len(r) - 1 - i]
		if z >= s - m:
			print(i + 1)
			h = 1
			break
if h == 0:
	print(-1)


(n, m) = map(int, input().split())
su = 0
a = []
for i in range(n):
	(x, y) = map(int, input().split())
	a.append(x - y)
	su += x
a.sort()
t = su - m
a.reverse()
if sum(a) < t:
	ans = -1
else:
	ans = 0
	i = 0
	while t > 0:
		ans += 1
		t -= a[i]
		i += 1
print(ans)


(n, m) = [int(x) for x in input().split(' ')]
difference_size = []
sum_original = 0
sum_compressed = 0
for i in range(n):
	size = [int(y) for y in input().split(' ')]
	sum_original = sum_original + size[0]
	sum_compressed += size[1]
	difference_size.append(size[0] - size[1])
if sum_compressed > m:
	print(-1)
elif sum_original <= m:
	print(0)
else:
	difference_size = sorted(difference_size, reverse=True)
	count1 = 0
	count2 = 0
	difference_sum = sum_original - m
	for a in difference_size:
		count1 += a
		count2 += 1
		if count1 >= difference_sum:
			break
	print(count2)


(n, m) = [int(x) for x in input().split()]
sum = 0
diffs = []
for _ in range(n):
	(a, b) = [int(x) for x in input().split()]
	diffs.append(b - a)
	sum = sum + a
diffs.sort()
index = 0
count = 0
while sum > m and index < len(diffs):
	sum = sum + diffs[index]
	index = index + 1
if sum > m:
	print(-1)
else:
	print(index)


n = input().split()
m = int(n[1])
n = int(n[0])
mat = [0] * n
total = compress = 0
for i in range(n):
	temp = input().split()
	total += int(temp[0])
	compress += int(temp[1])
	mat[i] = int(temp[0]) - int(temp[1])
mat.sort(reverse=True)
for i in range(n - 1):
	mat[i + 1] += mat[i]
if total <= m:
	print(0)
elif m < compress:
	print(-1)
else:
	diff = total - m
	for i in range(n):
		if diff <= mat[i]:
			print(i + 1)
			break


import sys
(n, m) = map(int, sys.stdin.readline().split())
d = {}
(s1, s2) = (0, 0)
for i in range(n):
	(ai, bi) = map(int, sys.stdin.readline().split())
	r = ai - bi
	if r not in d:
		d[r] = 0
	d[r] += 1
	s1 += ai
	s2 += bi
if s2 > m:
	r = -1
else:
	keys = list(d.keys())
	keys.sort(reverse=True)
	r = 0
	for k in keys:
		if s1 <= m:
			break
		if k * d[k] <= s1 - m:
			kolvo = d[k]
		else:
			kolvo = (s1 - m) // k
			if s1 - k * kolvo > m:
				kolvo += 1
		r += kolvo
		s1 -= k * kolvo
print(r)


import sys
import io, os
input = sys.stdin.buffer.readline
(n, m) = map(int, input().split())
C = []
s = 0
for i in range(n):
	(a, b) = map(int, input().split())
	c = a - b
	C.append(c)
	s += a
if s <= m:
	print(0)
	exit()
C.sort(reverse=True)
for (i, c) in enumerate(C):
	if s - c <= m:
		print(i + 1)
		exit()
	else:
		s -= c
else:
	print(-1)


input_str = input()
(n, m) = map(lambda x: int(x), input_str.split())
a = []
b = []
diff = []
for i in range(n):
	input_str = input()
	a.append(int(input_str.split()[0]))
	b.append(int(input_str.split()[1]))
	diff.append(a[-1] - b[-1])
vals = list(zip(zip(a, b), diff))
song_sum_comp = sum(b)
if song_sum_comp > m:
	print(-1)
vals.sort(key=lambda tup: tup[1])
min_rem = 0
song_sum = sum(a)
if song_sum <= m:
	print(0)
else:
	for el in reversed(vals):
		song_sum -= el[1]
		min_rem += 1
		if song_sum <= m:
			print(min_rem)
			break


[n, m] = [int(c) for c in input().split()]
songs = []
original = 0
need = 0
for c in range(n):
	songs.append([int(c) for c in input().split()])
	original += songs[-1][0]
songs = sorted(songs, key=lambda x: x[1] - x[0])
index = 0
while original > m and index < n:
	original -= songs[index][0] - songs[index][1]
	need += 1
	index += 1
if index == n and original > m:
	print(-1)
else:
	print(need)


q = [int(i) for i in input().split()]
n = q[0]
m = q[1]
ar = []
for i in range(0, n):
	x = [int(i) for i in input().split()]
	ar.append(x)
x1 = 0
x2 = 0
for i in ar:
	x1 += i[0]
	x2 += i[1]
if x1 <= m:
	print(0)
	exit()
if x2 > m:
	print(-1)
	exit()
if x2 == m:
	print(n)
	exit()
ard = []
ard1 = []
for i in ar:
	ard.append(i[0] - i[1])
ard1 = []
for i in range(0, len(ard)):
	ard1.append(ard[i])
ard1.sort()
ard1.reverse()
sum1 = x1
ard2 = []
for i in ard:
	ard2.append(i)
c = 0
for i in ard1:
	sum1 = sum1 - i
	c += 1
	if sum1 <= m:
		print(c)
		exit()


(n, m) = map(int, input().split())
mas = []
q = 0
for i in range(n):
	(a, b) = map(int, input().split())
	q += a
	mas.append(a - b)
mas.sort()
mas.reverse()
if q - sum(mas) > m:
	print(-1)
else:
	i = 0
	while q > m:
		q -= mas[i]
		i += 1
	print(i)


(n, m) = map(int, input().split())
arr = []
sumx = 0
for i in range(n):
	(a, b) = map(int, input().split())
	arr.append(a - b)
	sumx += a
arr.sort()
if sumx <= m:
	print(0)
else:
	j = n - 1
	while j >= 0 and sumx > m:
		sumx -= arr[j]
		j -= 1
	if sumx > m and j == -1:
		print(-1)
	else:
		print(n - 1 - j)


(a, b) = list((int(x) for x in input().split()))
of = []
cf = []
diff = []
for _ in range(a):
	(p, q) = list((int(x) for x in input().split()))
	of.append(p)
	cf.append(q)
t = sum(of) - b
if b < sum(cf):
	print(-1)
else:
	for i in range(a):
		diff.append(of[i] - cf[i])
	diff.sort(reverse=True)
	i = 0
	while t > 0:
		t -= diff[i]
		i += 1
	print(i)


def sol():
	(n, size) = list(map(int, input().split()))
	data = [0] * n
	s = 0
	for i in range(n):
		(a, b) = list(map(int, input().split()))
		s += a
		data[i] = b - a
	data.sort()
	d = 0
	while s > size and d < n:
		s += data[d]
		d += 1
	if s > size:
		print(-1)
	else:
		print(d)
sol()


(n, m) = map(int, input().split())
a = list((tuple(map(int, input().split())) for i in range(n)))
if sum((i[1] for i in a)) > m:
	print(-1)
else:
	c = sorted((i[0] - i[1] for i in a))
	s = sum((i[0] for i in a))
	i = n - 1
	while s > m:
		s -= c[i]
		i -= 1
	print(n - i - 1)


(n, m) = [int(x) for x in input().split()]
x_array = []
y_array = []
diff_array = []
for _ in range(n):
	(x, y) = [int(x) for x in input().split()]
	x_array += [x]
	y_array += [y]
	diff_array += [x - y]
t = sum(x_array)
if sum(y_array) > m:
	print(-1)
elif sum(x_array) <= m:
	print(0)
else:
	diff_array.sort(reverse=True)
	count = 0
	m = t - m
	while m > 0:
		m -= diff_array[count]
		count += 1
	print(count)


(n, m) = list(map(int, input().split()))
po = []
ps = []
diffs = []
for i in range(n):
	(a, b) = list(map(int, input().split()))
	po.append(a)
	ps.append(b)
	diffs.append(a - b)
tlen = sum(po)
tslen = sum(ps)
diffs.sort(reverse=True)
if sum(po) <= m:
	print(0)
	return
if sum(ps) > m:
	print(-1)
	return
r = sum(po)
cnt = 0
for d in diffs:
	cnt += 1
	r -= d
	if r <= m:
		print(cnt)
		return


def main():
	(n, m) = list(map(int, input().split()))
	songs = list()
	reduce = list()
	for i in range(n):
		(a, b) = list(map(int, input().split()))
		songs.append([a, b])
		tmp = a - b
		reduce.append(tmp)
	sum = 0
	min = 0
	cnt = 0
	reduce.sort()
	reduce = reduce[::-1]
	for song in songs:
		sum += song[0]
		min += song[1]
	if min > m:
		print(-1)
	else:
		while sum > m:
			sum -= reduce[cnt]
			cnt += 1
		print(cnt)
main()


import sys
inp = sys.stdin.readlines()
ii = 0
out = []
(n, m) = map(int, inp[ii].split())
ii += 1
regspace = 0
compspace = 0
diffs = []
for song in range(n):
	(reg, comp) = map(int, inp[ii].split())
	ii += 1
	diffs.append(reg - comp)
	regspace += reg
	compspace += comp
ans = 0
diffs.sort(reverse=True)
needed = regspace - m
cnt = 0
if sum(diffs) < needed:
	ans = -1
if regspace <= m:
	ans = 1
compressed = 0
for x in range(n):
	if compressed + diffs[x] < needed:
		compressed += diffs[x]
		cnt += 1
	else:
		break
if ans == 0:
	sys.stdout.write(str(cnt + 1))
elif ans == -1:
	sys.stdout.write(str(-1))
elif ans == 1:
	sys.stdout.write(str(0))


(n, m) = map(int, input().split())
(l, s1, s2) = ([], 0, 0)
for i in range(n):
	(a, b) = map(int, input().split())
	s1 += a
	s2 += b
	l.append(a - b)
l = sorted(l, reverse=True)
if s2 > m:
	print(-1)
else:
	(i, ans) = (0, 0)
	while s1 > m:
		s1 -= l[i]
		i += 1
		ans += 1
	print(ans)


(n, m) = map(int, input().split())
res = []
su = 0
for i in range(n):
	(a, b) = map(int, input().split())
	res.append(b - a)
	su += a
res.sort()
i = 0
while su > m and i < len(res):
	su += res[i]
	i += 1
if i >= len(res) and su > m:
	print(-1)
else:
	print(i)


from sys import stdin, stdout
from collections import Counter
import math
I = lambda : map(int, stdin.readline().split())
I1 = lambda : stdin.readline()
(n, m) = I()
(a, b) = ([], [])
for i in range(n):
	(x, y) = I()
	a.append(x)
	b.append(x - y)
x = sum(a)
if x <= m:
	print(0)
elif x - sum(b) > m:
	print(-1)
else:
	b.sort(reverse=True)
	ans = 0
	for i in b:
		x -= i
		ans += 1
		if x <= m:
			break
	print(ans)


(n, m) = map(int, input().split())
(D, s) = ([0] * (n + 1), 0)
for i in range(n):
	(a, b) = map(int, input().split())
	(D[i], s) = (a - b, s + a)
for (i, d) in enumerate(sorted(D, reverse=True)):
	if s <= m:
		print(i)
		break
	s -= d
else:
	print(-1)


import sys
input = lambda : sys.stdin.readline().strip('\r\n')
a = 0
b = 0
diff = []
(n, m) = map(int, input().split())
for i in range(n):
	(ai, bi) = map(int, input().split())
	a += ai
	b += bi
	diff.append(ai - bi)
init_sum = a
final_sum = b
if m < final_sum:
	print(-1)
else:
	diff.sort(reverse=True)
	i = 0
	while init_sum > m:
		init_sum -= diff[i]
		i += 1
	print(i)


from sys import stdin

def arr_inp(n):
	if n == 1:
		return [int(x) for x in stdin.readline().split()]
	elif n == 2:
		return [float(x) for x in stdin.readline().split()]
	else:
		return list(stdin.readline()[:-1])

def arr_2d(n):
	return [[int(x) for x in stdin.readline().split()] for i in range(n)]

def get_col(arr, i):
	return [row[i] for row in arr]
(n, m) = arr_inp(1)
a = sorted(arr_2d(n), key=lambda x: x[0] - x[1], reverse=True)
(s, ans) = (sum(get_col(a, 0)), 0)
for i in range(n):
	if s <= m:
		break
	else:
		s -= a[i][0] - a[i][1]
		ans += 1
print(-1 if s > m else ans)


(n, m) = [int(x) for x in input().split()]
a = []
b = []
v = []
for i in range(n):
	(l, r) = [int(x) for x in input().split()]
	a.append(l)
	b.append(r)
	v.append(l - r)
if sum(a) <= m:
	print(0)
	exit()
if sum(b) > m:
	print(-1)
	exit()
v.sort()
v = v[::-1]
pre = []
pre.append(v[0])
for i in range(1, n):
	pre.append(pre[i - 1] + v[i])
suma = sum(a)
c = 0
for i in range(n):
	if suma - pre[i] <= m:
		c = i + 1
		break
print(c)


(n, m) = map(int, input().split())
sol = []
for _ in range(n):
	(a, b) = map(int, input().split())
	sol.append([a, b, a - b])
sol.sort(key=lambda x: x[2], reverse=True)
c = 0
h = 0
for i in sol:
	h += i[0]
i = 0
if h == m:
	print(0)
else:
	ans = 0
	f = 0
	for i in range(n):
		if h <= m:
			ans = i
			f = 1
			break
		else:
			h -= sol[i][0] - sol[i][1]
	if f == 1:
		print(ans)
	elif f == 0:
		if h <= m:
			print(n)
		else:
			print(-1)


a = input()
a = a.split()
check = int(a[0])
space = int(a[1])
content = []
for i in range(check):
	x = input()
	x = x.split()
	x[0] = int(x[0])
	x[1] = int(x[1])
	content.append(x)
usedspace = 0
newcontent = []
for i in content:
	newcontent.append(i[0] - i[1])
for i in content:
	usedspace += int(i[0])
spaceover = (space - usedspace) * -1
newcontent.sort()
newcontent = newcontent[::-1]
tempsum = 0
i = 0
trigger = 0
while tempsum < spaceover:
	if i < check:
		tempsum += newcontent[i]
		i += 1
	else:
		print(-1)
		trigger = 1
		break
if trigger == 0:
	print(i)


(n, m) = map(int, input().split())
maxm = minm = 0
d = []
for i in range(n):
	(a, b) = map(int, input().split())
	maxm = maxm + a
	minm = minm + b
	d.append(a - b)
if minm > m:
	print(-1)
elif maxm <= m:
	print(0)
else:
	d = sorted(d, reverse=True)
	cnt = 0
	for i in range(n):
		maxm = maxm - d[i]
		cnt = cnt + 1
		if maxm <= m:
			break
	print(cnt)


(n, m) = map(int, input().split())
s = list()
cnt = 0
cnt_tmp = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	cnt += a
	cnt_tmp += a - b
	s.append(a - b)
tmp = sorted(s, reverse=True)
if cnt > m:
	if cnt - cnt_tmp > m:
		print(-1)
	else:
		for i in range(n):
			if cnt - tmp[i] <= m:
				print(i + 1)
				break
			else:
				cnt -= tmp[i]
else:
	print(0)


(n, m) = input().split()
n = int(n)
m = int(m)
a = []
b = []
c = []
ans = 0
for i in range(n):
	(x, y) = input().split()
	a.append(int(x))
	b.append(int(y))
	c.append(int(x) - int(y))
if sum(b) > m:
	print(-1)
else:
	tmp = sum(a)
	c.sort()
	c = c[::-1]
	while tmp > m:
		tmp += -c[ans]
		ans += 1
	print(ans)


inp = list(map(int, input().split()))
n = inp[0]
m = inp[1]
original_size = []
compressed_size = []
for i in range(0, n):
	inp = list(map(int, input().split()))
	original_size.append(inp[0])
	compressed_size.append(inp[1])
original_sum = sum(original_size)
if original_sum <= m:
	print(0)
	exit()
compressed_sum = sum(compressed_size)
if compressed_sum > m:
	print(-1)
	exit()
diff = original_sum - m
diff_list = []
for i in range(len(original_size)):
	diff_list.append(original_size[i] - compressed_size[i])
diff_list.sort(reverse=True)
count = 0
while diff > 0:
	diff = diff - diff_list[count]
	count = count + 1
print(count)


(n, m) = map(int, input().split())
l = []
for i in range(n):
	(a, b) = map(int, input().split())
	l.append([a, b])
x = []
sum1 = 0
for i in range(n):
	sum1 += l[i][0]
	x.append(l[i][0] - l[i][1])
x.sort()
x.reverse()
c = 0
while c < n and sum1 > m:
	sum1 = sum1 - x[c]
	c = c + 1
if sum1 > m:
	print('-1')
else:
	print(c)


(n, c) = map(int, input().split())
N = []
diff = []
sum1 = 0
for i in range(n):
	(actual, compresed) = map(int, input().split())
	sum1 += actual
	asd = actual - compresed
	if asd > 0:
		diff.append(asd)
diff.sort()
z = len(diff)
ans = 0
if sum1 <= c:
	print(ans)
else:
	for i in diff[::-1]:
		sum1 -= i
		ans += 1
		if sum1 <= c:
			print(ans)
			break
	else:
		print(-1)


import bisect
from itertools import accumulate
(n, m) = map(int, input().split())
shrinks = []
total = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	total += a
	shrinks.append(a - b)
shrinks.sort(reverse=True)
acc = list(accumulate(shrinks))
if total <= m:
	print(0)
elif total - acc[-1] > m:
	print(-1)
else:
	to_shrink = total - m
	i = bisect.bisect_left(acc, to_shrink)
	print(i + 1)


(n, m) = map(int, input().split())
l1 = []
l2 = []
l3 = []
for i in range(n):
	(l, r) = map(int, input().split())
	l1.append(l)
	l2.append(r)
	l3.append(l - r)
a1 = sum(l1)
a2 = sum(l2)
l3 = sorted(l3, reverse=True)
if a2 > m:
	print('-1')
elif a1 <= m:
	print('0')
else:
	c = 0
	j = 0
	while a1 > m:
		a1 -= l3[j]
		j += 1
		c += 1
	print(c)


import collections as cc
import sys
I = lambda : list(map(int, input().split()))
(n, m) = I()
a = []
b = []
dif = []
for i in range(n):
	(x, y) = I()
	a.append(x)
	b.append(y)
	dif.append(x - y)
if sum(a) <= m:
	print(0)
else:
	ans = 0
	d = abs(m - sum(a))
	dif.sort(reverse=True)
	su = sum(a)
	for i in range(n):
		if su - dif[i] <= m:
			su -= dif[i]
			ans += 1
			break
		else:
			su -= dif[i]
			ans += 1
	if su <= m:
		print(ans)
	else:
		print(-1)


(n, m) = map(int, input().split())
un = []
co = []
d = []
for i in range(n):
	(u, c) = map(int, input().split())
	un.append(u)
	co.append(c)
	d.append(u - c)
curr_sum = sum(un)
cou = 0
d.sort(reverse=True)
if sum(un) <= m:
	print('0')
else:
	for i in range(n):
		if curr_sum <= m:
			break
		else:
			curr_sum = curr_sum - d[i]
			cou += 1
	if curr_sum <= m:
		print(cou)
	else:
		print('-1')


(n, m) = map(int, input().split())
s = 0
d = []
for i in range(n):
	(a, b) = map(int, input().split())
	s += a
	d.append(a - b)
if s <= m:
	print(0)
else:
	d = sorted(d)
	diff = s - m
	count = 0
	for i in range(len(d) - 1, -1, -1):
		diff -= d[i]
		count += 1
		if diff <= 0:
			print(count)
			exit()
	print(-1)


(n, m) = [int(i) for i in input().split()]
a = []
(un, comp) = (0, 0)
for _ in range(n):
	temp = [int(i) for i in input().split()]
	tmp = temp[0] - temp[1]
	temp.append(tmp)
	a.append(temp)
	un += temp[0]
	comp += temp[1]
if un <= m:
	print('0')
elif comp > m:
	print('-1')
else:
	a.sort(key=lambda a: (-a[2], -a[1]))
	count = 0
	for i in range(n):
		un = un - a[i][0] + a[i][1]
		count += 1
		if un <= m:
			break
	print(count)


(N, M) = [int(x) for x in input().split()]
arr1 = []
arr2 = []
sub = []
count = 0
for i in range(0, N):
	(A, B) = [int(x) for x in input().split()]
	arr1.append(A)
	arr2.append(B)
else:
	sum1 = sum(arr1)
	diff = sum1 - M
	for i in range(0, len(arr1)):
		sub.append(arr1[i] - arr2[i])
	sub.sort()
	sub.reverse()
	for i in range(0, len(sub)):
		if diff > 0:
			diff = diff - sub[i]
			count = count + 1
if sum(arr2) > M:
	print('-1')
else:
	print(count)


(n, m) = map(int, input().split())
res = 0
temp = []
for i in range(n):
	(a, b) = map(int, input().split())
	res += a
	temp.append(a - b)
if res > m:
	temp.sort(reverse=True)
	i = 0
	while res > m:
		if i > len(temp) - 1:
			print(-1)
			exit()
		res -= temp[i]
		i += 1
	print(i)
else:
	print(0)


io = input().split()
n = int(io[0])
m = int(io[1])
totalSize = 0
arr = []
for i in range(n):
	io = input().split()
	a = int(io[0])
	b = int(io[1])
	totalSize += a
	arr.append(a - b)
arr.sort(reverse=True)
count = 0
for i in arr:
	if totalSize > m:
		totalSize -= i
		count += 1
if totalSize > m:
	print(-1)
else:
	print(count)


(n, m) = map(int, input().split())
L = []
s = 0
for i in range(n):
	(a, b) = map(int, input().split())
	s = s + a
	L.append(a - b)
if s > sum(L) + m:
	print(-1)
else:
	L = sorted(L, reverse=True)
	c = 0
	for i in range(n):
		if s <= m:
			break
		c += 1
		s = s - L[i]
	print(c)


(n, m) = map(int, input().split())
a = []
b = []
for n0 in range(n):
	(i, j) = map(int, input().split())
	a.append(i)
	b.append(j)
if sum(b) > m:
	print('-1')
else:
	tot = []
	for i in range(n):
		tot.append(b[i] - a[i])
	tot.sort()
	t = sum(a)
	c = 0
	i = 0
	while t > m:
		t = t + tot[i]
		c += 1
		i += 1
	print(c)


(n, m) = list(map(int, input().split()))
A = [0] * n
z = 0
for i in range(n):
	(a, b) = list(map(int, input().split()))
	z += a
	A[i] = b - a
A.sort()
k = 0
for i in range(n):
	if z <= m:
		break
	k += 1
	z += A[i]
if z <= m:
	print(k)
else:
	print(-1)


R = lambda : map(int, input().split())
(n, fd) = R()
(bcl, acl, dl) = (list(), list(), list())
for _ in range(n):
	(bc, ac) = R()
	bcl.append(bc)
	acl.append(ac)
	dl.append(bc - ac)
if sum(acl) > fd:
	print(-1)
	exit(0)
sumbc = sum(bcl)
dl.sort()
finaldiff = sumbc - fd
count = 0
while True:
	if finaldiff <= 0:
		break
	else:
		count += 1
		finaldiff -= dl[-1]
	dl.pop()
print(count)


__author__ = 'Esfandiar'
(n, m) = map(int, input().split())
a = list()
su = 0
for i in range(n):
	(c, d) = map(int, input().split())
	a.append(c - d)
	su += c
if su <= m:
	print(0)
else:
	a = sorted(a, reverse=True)
	res = 0
	i = 0
	while i < n and su > m:
		su -= a[i]
		i += 1
		res += 1
	print(res if su <= m else -1)


(n, s) = list(map(int, input().split()))
(a, b, c) = ([], [], [])
for i in range(n):
	(p, q) = list(map(int, input().split()))
	a.append(p)
	b.append(q)
	c.append(p - q)
c.sort()
l = sum(b)
if l > s:
	print(-1)
else:
	i = 0
	while i < n and l + c[i] <= s:
		l += c[i]
		i = i + 1
	print(n - i)


(n, m) = [int(x) for x in input().split()]
non_com = 0
com = 0
_y = list()
for i in range(n):
	(x, y) = [int(x) for x in input().split()]
	non_com += x
	com += y
	_y.append(x - y)
_y = sorted(_y)
if com > m:
	print(-1)
elif non_com <= m:
	print(0)
else:
	while _y:
		non_com -= _y.pop()
		if non_com <= m:
			print(n - len(_y))
			break


import sys
sys.setrecursionlimit(2000)
from collections import Counter
from functools import reduce
(n, m) = [int(val) for val in sys.stdin.readline().split()]
songs = []
for song in range(n):
	(a, b) = [int(val) for val in sys.stdin.readline().split()]
	songs.append((a, b, a - b))
songs.sort(key=lambda x: x[2], reverse=True)
summ = sum([val[0] for val in songs])
ind = 0
while ind < n and summ > m:
	summ -= songs[ind][2]
	ind += 1
if summ > m:
	print(-1)
else:
	print(ind)


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
tot = 0
r = []
for i in range(n):
	(a, b) = map(int, input().split())
	r.append([a - b, a, b])
	tot += a
r.sort(reverse=True)
i = 0
while tot > m and i < n:
	tot -= r[i][0]
	i += 1
if tot <= m:
	print(i)
else:
	print(-1)


def main():
	(n, m) = map(int, input().split())
	difs = [None] * n
	sum_a = 0
	for i in range(n):
		(a, b) = map(int, input().split())
		sum_a += a
		difs[i] = a - b
	if sum_a <= m:
		print(0)
		return
	difs.sort(reverse=True)
	for i in range(n):
		sum_a -= difs[i]
		if sum_a <= m:
			print(i + 1)
			return
	print(-1)
main()


(n, m) = map(int, input().split())
a = []
s = 0
k = 0
a_add = a.append
for i in range(n):
	(x, y) = map(int, input().split())
	a_add(x - y)
	s += x
	k += y
if k > m:
	print(-1)
	quit()
a.sort(reverse=True)
f = s - m
cnt = 0
res = 0
i = 0
for i in range(n):
	if res >= f:
		print(cnt)
		quit()
	res += a[i]
	cnt += 1
if res >= f:
	print(cnt)
	quit()
print(-1)


(n, m) = map(int, input().split())
a = 0
c = []
for _ in range(n):
	(x, y) = map(int, input().split())
	a += x
	c.append(x - y)
c.sort()
c.reverse()
k = 0
while k < n and a > m:
	a -= c[k]
	k += 1
if a > m:
	print(-1)
else:
	print(k)


(n, m) = (int(x) for x in input().split())
s1 = 0
s2 = 0
razn = []
ans = 0
for i in range(n):
	(a, b) = (int(x) for x in input().split())
	razn.append(a - b)
	s1 += a
	s2 += b
if s2 > m:
	print(-1)
else:
	ost = s1 - m
	razn = sorted(razn)
	while ost > 0:
		ost -= razn[-1]
		ans += 1
		razn.pop(-1)
	print(ans)


(N, M) = map(int, input().split())
total = 0
gaps = []
for n in range(N):
	(a, b) = map(int, input().split())
	total += a
	gaps += [a - b]
gaps.sort()
ans = 0
while total > M and gaps != []:
	total -= gaps.pop()
	ans += 1
if total <= M:
	print(ans)
else:
	print(-1)


(n, m) = map(int, input().split())
(a, b, ans1) = ([], [], [])
for i in range(n):
	(c, d) = map(int, input().split())
	a.append(c)
	b.append(d)
	ans1.append(c - d)
if sum(b) > m:
	print(-1)
elif sum(b) == m:
	print(n)
else:
	ans1.sort()
	i = n - 1
	p = sum(a)
	ans = 0
	while i > -1 and p > m:
		p -= ans1[i]
		i -= 1
		ans += 1
	print(ans)


arrayB = []
count = _sum = 0
flag = False
(n, m) = map(int, input().split())
for i in range(0, n):
	(a, b) = map(int, input().split())
	_sum += a
	arrayB.append(a - b)
arrayB.sort()
if _sum <= m:
	print(count)
else:
	i = n - 1
	while i > -1:
		_sum -= arrayB[i]
		count += 1
		if _sum <= m:
			flag = True
			break
		i -= 1
	if flag == True:
		print(count)
	else:
		print(-1)


(n, m) = map(int, input().split())
c = [tuple(map(int, input().split())) for _ in range(n)]
if sum([c[i][1] for i in range(n)]) > m:
	print(-1)
else:
	s = sum((c[i][0] for i in range(n)))
	if s <= m:
		print(0)
	else:
		c.sort(key=lambda x: x[1] - x[0])
		for i in range(n):
			s -= c[i][0] - c[i][1]
			if s <= m:
				print(i + 1)
				break


def main():
	(n, m) = map(int, input().split())
	a = []
	bsum = 0
	add = 0
	for _ in range(n):
		(x, y) = map(int, input().split())
		a.append(x - y)
		add += x
		bsum += y
	if bsum > m:
		print(-1)
		return
	a.sort(reverse=True)
	ans = 0
	for i in range(len(a)):
		if add <= m:
			break
		ans += 1
		add -= a[i]
	print(ans)
	return
main()


import math
from decimal import Decimal
import heapq

def na():
	n = int(input())
	b = [int(x) for x in input().split()]
	return (n, b)

def nab():
	n = int(input())
	b = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	return (n, b, c)

def dv():
	(n, m) = map(int, input().split())
	return (n, m)

def dva():
	(n, m) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	return (n, m, b)

def eratosthenes(n):
	sieve = list(range(n + 1))
	for i in sieve:
		if i > 1:
			for j in range(i + i, len(sieve), i):
				sieve[j] = 0
	return sorted(set(sieve))

def nm():
	n = int(input())
	b = [int(x) for x in input().split()]
	m = int(input())
	c = [int(x) for x in input().split()]
	return (n, b, m, c)

def dvs():
	n = int(input())
	m = int(input())
	return (n, m)
(n, m) = map(int, input().split())
a = []
tc = 0
for i in range(n):
	(j, b) = map(int, input().split())
	tc += j
	a.append(j - b)
a.sort(reverse=True)
if tc <= m:
	print(0)
else:
	tc -= m
	ans = 0
	for i in a:
		tc -= i
		ans += 1
		if tc <= 0:
			print(ans)
			exit()
	print(-1)


(n, m) = map(int, input().split())
a = []
b = []
for i in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	b.append(y)
s1 = 0
for x in a:
	s1 += x
c = []
for i in range(n):
	c.append(a[i] - b[i])
k = 0
c.sort()
d = s1 - m
if d <= 0:
	print(0)
else:
	d1 = 0
	i = len(c) - 1
	while i >= 0 and d1 < d:
		d1 += c[i]
		k += 1
		i -= 1
	if i < 0 and d1 < d:
		print(-1)
	else:
		print(k)


(nsongs, cap) = map(int, input().split())
saving = [0] * nsongs
total_o = 0
total_c = 0
for i in range(nsongs):
	(o, c) = map(int, input().split())
	saving[i] = o - c
	total_o += o
	total_c += c
diff = total_o - cap
saving.sort(reverse=True)
cnt = 0
i = 0
if total_c > cap:
	print(-1)
else:
	while diff > 0:
		diff -= saving[i]
		cnt += 1
		i += 1
	print(cnt)


(n, m) = [int(x) for x in input().split()]
a = []
b = []
d = []
count = 0
total = 0
for i in range(n):
	(p, q) = [int(x) for x in input().split()]
	a.append(p)
	b.append(q)
	d.append(p - q)
d.sort()
c = sum(a)
if c <= m:
	print('0')
elif sum(b) > m:
	print('-1')
else:
	for i in range(n - 1, -1, -1):
		total = total + d[i]
		count += 1
		if total >= c - m:
			print(count)
			break


(n, m) = map(int, input().split())
a = []
b = []
d = []
for i in range(n):
	(x, y) = map(int, input().split())
	a += [x]
	b += [y]
	d += [abs(x - y)]
if sum(b) > m:
	print(-1)
else:
	res = sum(a) - m
	d.sort(reverse=True)
	i = 0
	while res > 0:
		res -= d[i]
		i += 1
	print(i)


(n, m) = [int(a) for a in input().split()]
b = []
ar = []
s = 0
c = []
for i in range(0, n):
	(x, y) = [int(a) for a in input().split()]
	k = x - y
	ar.append(k)
	b.append(x)
	c.append(y)
ar.sort()
h = sum(b)
l = sum(c)
if l <= m:
	if h > m:
		for p in range(n - 1, -1, -1):
			h = h - ar[p]
			s = s + 1
			if h <= m:
				break
		print(s)
	else:
		print('0')
else:
	print('-1')


from __future__ import print_function
import sys
import math
import os.path
from copy import deepcopy
from functools import reduce
from collections import Counter, ChainMap, defaultdict
from itertools import cycle, chain
from queue import Queue, PriorityQueue, deque
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, nlargest, nsmallest
import bisect
from statistics import mean, mode, median, median_low, median_high
sys.setrecursionlimit(10 ** 9)

def log(*args, **kwargs):
	print(*args, file=sys.stderr, **kwargs)

def ni():
	return map(int, input().split())

def nio(offset):
	return map(lambda x: int(x) + offset, input().split())

def nia():
	return list(map(int, input().split()))

def toString(aList, sep=' '):
	return sep.join((str(x) for x in aList))

def toMapInvertIndex(aList):
	return {k: v for (v, k) in enumerate(aList)}

def sortId(arr):
	return sorted(range(len(arr)), key=lambda k: arr[k])
(n, m) = ni()
a = [0] * n
b = [0] * n
c = [0] * n
for i in range(n):
	(a[i], b[i]) = ni()
	c[i] = a[i] - b[i]
cid = sortId(c)
sb = sum(b)
if sb > m:
	print(-1)
else:
	s = sum(a)
	count = 0
	while s > m:
		s -= c[cid[-1 - count]]
		count += 1
	print(count)


import sys
s = input()
numbers = list(map(int, s.split()))
espacio = numbers[1]
lista = []
for i in range(0, numbers[0]):
	line = input()
	ran = list(map(int, line.split()))
	lista.append(ran)
restas = []
checkComp = 0
total = 0
compressed = 0
for x in range(len(lista)):
	restas.append(lista[x][0] - lista[x][1])
	checkComp = checkComp + lista[x][1]
	total = total + lista[x][0]
if checkComp > espacio:
	print(-1)
else:
	restas.sort()
	while True:
		if total <= espacio:
			print(compressed)
			break
		else:
			total = total - restas[len(restas) - 1]
			del restas[len(restas) - 1]
			compressed += 1


(n, m) = map(int, input().split())
l = []
for j in range(n):
	s = list(map(int, input().split()))
	s.append(s[0] - s[1])
	s.reverse()
	l.append(s)
	j += 1
l.sort()
l.reverse()
k = 0
a = 0
while k < n:
	a = a + l[k][2]
	k += 1
b = a - m
i = 0
d = 0
while i < n:
	if d >= b:
		break
	else:
		d = d + l[i][0]
		i += 1
if i == n and d < b:
	print(-1)
else:
	print(i)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def get_v(l, p):
	if p < 0:
		return 0
	if p >= len(l):
		return 0
	return l[p]

def main():
	(n, k) = inlt()
	data = []
	for i in range(n):
		(a, b) = inlt()
		data.append((a, b))
	total = sum([d[0] for d in data])
	data.sort(key=lambda b: b[0] - b[1], reverse=True)
	c = 0
	while total > k and c < len(data):
		total -= data[c][0] - data[c][1]
		c += 1
	print(c if total <= k else -1)
main()


def main():
	(a, lim) = map(int, input().split())
	chans = []
	tt = 0
	for i in range(a):
		(av, ap) = map(int, input().split())
		tt += av
		chans.append(av - ap)
	if tt <= lim:
		print(0)
		return
	chans.sort(reverse=1)
	indice = 0
	try:
		while tt > lim:
			tt -= chans[indice]
			indice += 1
	except:
		print(-1)
		return
	print(indice)
main()


(n, m) = map(int, input().split())
b = 0
s = 0
d = []
for i in range(n):
	(v, ma) = map(int, input().split())
	b = b + v
	s = s + ma
	d.append(v - ma)
d.sort(reverse=True)
if b <= m:
	print(0)
elif s > m:
	print(-1)
else:
	c = 0
	res = 0
	for broj in d:
		c = c + broj
		res = res + 1
		if b - c <= m:
			print(res)
			break


(n, m) = map(int, input().split())
diff = []
bb = []
for _ in range(n):
	(a, b) = map(int, input().split())
	bb.append(a)
	diff.append(a - b)
diff.sort()
diff = diff[::-1]
h = sum(bb) - m
ans = False
for i in range(n):
	if h <= 0:
		ans = True
		print(i)
		break
	h -= diff[i]
	if h <= 0:
		ans = True
		print(i + 1)
		break
if not ans:
	print(-1)


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from math import sqrt, floor, factorial, gcd, log
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations
from math import gcd
from bisect import bisect
input = lambda : sys.stdin.readline().rstrip('\r\n')
read = lambda : list(map(int, input().strip().split(' ')))

def solve():
	(n, k) = read()
	cum = 0
	arr = []
	for _ in range(n):
		(a, b) = read()
		cum += a
		arr.append(a - b)
	arr.sort(reverse=True)
	if cum > k:
		ans = 0
		s = 0
		while ans < n and s < cum - k:
			s += arr[ans]
			ans += 1
		if s < cum - k:
			ans = -1
	else:
		ans = 0
	print(ans)
solve()


(n, m) = map(int, input().split())
songs = []
for i in range(n):
	songs.append(list(map(int, input().split())))
if sum([s[1] for s in songs]) > m:
	print(-1)
	exit()
songs.sort(key=lambda x: x[0] - x[1], reverse=True)
initsum = sum([s[0] for s in songs])
reqdreduction = initsum - m
diff = 0
count = 0
while diff < reqdreduction:
	diff += songs[count][0] - songs[count][1]
	count += 1
print(count)


(n, m) = map(int, input().split())
arrb = []
arrbc = 0
arrac = 0
count = 0
for i in range(n):
	(a, b) = map(int, input().split())
	arrac += a
	arrbc += b
	sub = a - b
	arrb.append(sub)
arrb.sort(reverse=True)
if arrbc > m:
	print(-1)
else:
	i = 0
	k = arrac - m
	while k > 0:
		k -= arrb[i]
		i += 1
	print(i)


(n, m) = map(int, input().split())
u = []
sum1 = 0
sum2 = 0
for i in range(n):
	(a, b) = map(int, input().split())
	u.append(a - b)
	sum1 += a
	sum2 += b
if sum2 > m:
	print(-1)
	exit()
u.sort(reverse=1)
u.insert(0, 0)
for i in range(1, n + 1):
	u[i] += u[i - 1]
R = n + 1
L = 0
while R - L > 1:
	M = (R + L) // 2
	if sum1 - u[M] <= m:
		R = M
	else:
		L = M
if sum1 - u[L] <= m:
	print(L)
else:
	print(R)


import sys
import math
from collections import defaultdict, deque
input = sys.stdin.readline

def inar():
	return [int(el) for el in input().split()]

def main():
	(n, m) = inar()
	tup = []
	sm = 0
	for i in range(n):
		(a, b) = inar()
		sm += a
		diff = a - b
		tup.append([diff, a, b])
	tup.sort(reverse=True)
	ans = 0
	i = 0
	while sm > m and i < n:
		sm -= tup[i][1]
		sm += tup[i][2]
		i += 1
		ans += 1
	if sm <= m:
		print(ans)
	else:
		print(-1)
main()


(n, m) = [int(i) for i in input().split()]
arr = []
for i in range(n):
	temp = [int(i) for i in input().split()]
	temp.append(temp[0] - temp[1])
	arr.append(temp)
sum = 0
for i in range(n):
	sum += arr[i][0]
count = 0
arr.sort(key=lambda arr: arr[2])
arr.reverse()
while sum > m and count < n:
	sum -= arr[count][2]
	count += 1
if sum <= m:
	print(count)
else:
	print(-1)


import sys
import math
flag = 0
(a, b) = map(int, input().split())
c = []
d = []
e = []
for i in range(a):
	(f, g) = map(int, input().split())
	c.append(f)
	d.append(g)
	e.append(f - g)
k = sum(c)
if k <= b:
	print(0)
elif k - sum(e) <= b:
	e.sort(reverse=True)
	s = 0
	for j in range(a):
		s += e[j]
		if k - s <= b:
			flag = 1
			break
	if flag:
		print(j + 1)
	else:
		print('-1')
else:
	print('-1')


[n, m] = [int(x) for x in input().split()]
a = []
b = []
for i in range(0, n):
	a.append([int(x) for x in input().split()])
for i in range(0, n):
	b.append([a[i][0] - a[i][1], i])
sumc0 = 0
sumc1 = 0
for i in range(0, n):
	sumc1 = sumc1 + a[i][1]
	sumc0 = sumc0 + a[i][0]
if sumc0 > m:
	if sumc1 < m:
		count = 0
		b = sorted(b, key=lambda x: x[0], reverse=True)
		for i in range(0, n):
			sumc0 = sumc0 - a[b[i][1]][0] + a[b[i][1]][1]
			count = count + 1
			if sumc0 <= m:
				print(count)
				break
	elif sumc1 == m:
		print(n)
	else:
		print(-1)
else:
	print(0)


temp = input()
temp = temp.split(' ')
n = int(temp[0])
m = int(temp[1])
dif = []
sum = 0
for x in range(n):
	temp = input()
	temp = temp.split()
	sum += int(temp[0])
	dif.append(int(temp[0]) - int(temp[1]))
dif.sort(reverse=True)
count = 0
for x in dif:
	if sum <= m:
		break
	count += 1
	sum = sum - x
if count == n and sum > m:
	count = -1
print(count)


def main():
	import sys
	(n, m) = [int(x) for x in sys.stdin.readline().strip('\n').split(' ')]
	s_pairs = []
	for i in range(0, n):
		tup = tuple((int(x) for x in sys.stdin.readline().strip('\n').split(' ')))
		s_pairs.append(tup)
	s_pairs = sorted(s_pairs, key=lambda x: x[0] - x[1], reverse=True)
	first_sum = sum((x[0] for x in s_pairs))
	rem = first_sum - m
	curr_sum = 0
	ans = 0
	if rem <= 0:
		print(ans)
		return
	for x in s_pairs:
		curr_sum += x[0] - x[1]
		ans += 1
		if curr_sum >= rem:
			break
	can_solve = curr_sum >= rem
	if can_solve:
		print(ans)
	else:
		print(-1)
main()


(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
a.sort(key=lambda x: x[1] - x[0])
x = 0
for i in a:
	x += i[0]
i = 0
while i != n and x > m:
	x -= a[i][0] - a[i][1]
	i += 1
if x > m:
	print(-1)
else:
	print(i)


(X, Original, Compress, SUM) = (list(map(int, input().split())), [], [], 0)
for i in range(X[0]):
	Temp = list(map(int, input().split()))
	Original.append(Temp[0])
	SUM += Temp[1]
	Compress.append(Temp[0] - Temp[1])
if SUM > X[1]:
	print(-1)
	exit()
Compress = sorted(Compress, reverse=True)
(i, SUM, Count) = (0, sum(Original), 0)
while SUM > X[1]:
	SUM -= Compress[i]
	i += 1
	Count += 1
print(Count)


import sys
(n, m) = map(int, input().split())
L = []
sm = 0
for i in range(n):
	(a, b) = map(int, input().split())
	L.append([a, b])
	sm += a
if sm <= m:
	print(0)
	sys.exit()

def sort(L1):
	return -(L1[0] - L1[1])
L.sort(key=sort)
for i in range(n):
	sm = sm - (L[i][0] - L[i][1])
	if sm <= m:
		print(i + 1)
		sys.exit()
print(-1)


(n, m) = map(int, input().split())
sum = 0
suma = 0
arr = [0] * n
for i in range(n):
	(a, b) = map(int, input().split())
	suma += a
	arr[i] = a - b
	sum += b
if sum > m:
	print(-1)
elif suma <= m:
	print(0)
else:
	comp = 0
	arr.sort(reverse=True)
	for i in range(n):
		comp += arr[i]
		if comp >= suma - m:
			print(i + 1)
			break
	else:
		print(n)


(n, m) = map(int, input().split())
ans = 0
temp = [0 for i in range(n)]
for i in range(n):
	(l, r) = map(int, input().split())
	ans += l
	temp[i] = l - r
temp.sort(reverse=True)
if ans <= m:
	print(0)
else:
	for i in range(n):
		ans -= temp[i]
		if ans <= m:
			print(i + 1)
			break
	else:
		print(-1)


(n, m) = [int(i) for i in input().split()]
A = []
diff = []
for i in range(n):
	(a, b) = [int(i) for i in input().split()]
	A.append(a)
	diff.append(a - b)
summ = 0
for i in A:
	summ += i
res = 0
diff.sort(reverse=True)
while summ > m and res < n:
	summ -= diff[res]
	res += 1
if summ <= m:
	print(res)
else:
	print(-1)


(n, m) = map(int, input().split())
x = []
y = []
c = 0
e = 0
for i in range(n):
	(a, b) = map(int, input().split())
	c = c + a
	e = e + b
	x.append(a)
	y.append(a - b)
y.sort()
y.reverse()
d = 0
j = 0
if e <= m:
	while c > m:
		d = d + 1
		c = c - y[j]
		j = j + 1
	print(d)
else:
	print('-1')


(n, m) = map(int, input().split())
s = 0
x = [0] * n
for i in range(n):
	(a, b) = map(int, input().split())
	s += a
	x[i] = a - b
x.sort()
x.reverse()
sm = 0
for i in range(n + 1):
	if s - sm <= m:
		print(i)
		break
	elif i == n:
		print(-1)
		break
	sm += x[i]


(n, m) = input().split()
(n, m) = (int(n), int(m))
(A, B) = ([], [])
totA = totB = 0
for _ in range(n):
	(a, b) = input().split()
	(a, b) = (int(a), int(b))
	totA += a
	totB += b
	A.append(a)
	B.append(b)
if m < totB:
	print(-1)
elif m == totB:
	print(n)
elif m >= totA:
	print(0)
else:
	target = totA - m
	diffs = [0] * n
	for i in range(n):
		diffs[i] = A[i] - B[i]
	diffs.sort(reverse=True)
	ans = 0
	for e in diffs:
		target -= e
		ans += 1
		if target <= 0:
			break
	print(ans)


from sys import stdin, stdout

def rint():
	return map(int, stdin.readline().split())
(n, m) = rint()
l = []
for i in range(n):
	(atemp, btemp) = rint()
	l.append([atemp, btemp])
l.sort(key=lambda x: x[0] - x[1], reverse=True)
cnt = 0
tot = 0
for i in range(n):
	tot += l[i][0]
over = tot - m
for i in range(n):
	r = l[i][0] - l[i][1]
	if over <= 0:
		print(cnt)
		exit()
	else:
		cnt += 1
		over -= r
if over <= 0:
	print(cnt)
else:
	print(-1)


from collections import deque
from math import ceil

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
(n, m) = mi()
a = []
s = 0
for i in range(n):
	(x, y) = mi()
	a.append(x - y)
	s += x
if s <= m:
	print('0')
else:
	x = s - m
	c = 0
	a.sort(reverse=True)
	for i in range(n):
		x -= a[i]
		c += 1
		if x <= 0:
			break
	if x > 0:
		print('-1')
	else:
		print(c)


(n, m) = map(int, input().split())
a = []
dif = []
for i in range(n):
	(x, y) = map(int, input().split())
	a.append(x)
	dif.append(x - y)
dif = sorted(dif)[::-1]
s = sum(a) - m
if s <= 0:
	print(0)
	exit()
for i in range(n):
	s -= dif[i]
	if s <= 0:
		print(i + 1)
		exit()
print(-1)


line = input().split()
(n, m) = (int(line[0]), int(line[1]))
arr = []
summ = 0
comm = 0
for i in range(n):
	line = input().split()
	arr.append(int(line[0]) - int(line[1]))
	summ += int(line[0])
	comm += int(line[1])
arr.sort(reverse=True)
if summ <= m:
	print('0')
elif comm > m:
	print('-1')
elif summ > m:
	(count, flag) = (0, 0)
	for i in range(n):
		summ -= arr[i]
		if summ <= m:
			flag = 1
			break
	if flag == 1:
		print(i + 1)
	else:
		print('-1')


(n, m) = map(int, input().split())
deltas = []
tot = 0
for i in range(n):
	(a, b) = map(int, input().split())
	tot += a
	deltas.append(a - b)
deltas.sort()
ans = 0
while deltas and tot > m:
	tot -= deltas.pop()
	ans += 1
if tot > m:
	print(-1)
else:
	print(ans)


def solve(arr, without, withh, n, m):
	if without <= m:
		return 0
	if withh > m:
		return -1
	if withh == m:
		return n
	reque = without - m
	arr.sort(reverse=True)
	i = 0
	count = 0
	while i < n and reque > 0:
		reque -= arr[i]
		count += 1
		i += 1
	if reque > 0:
		return -1
	return count
(n, m) = map(int, input().split())
arr = []
(without, withh) = (0, 0)
for i in range(n):
	(a, b) = map(int, input().split())
	without += a
	withh += b
	arr.append(a - b)
print(solve(arr, without, withh, n, m))


(t, m) = map(int, input().rstrip().split(' '))
a = [0] * t
b = [0] * t
d = [0] * t
i = 0
while i < t:
	(ai, bi) = map(int, input().rstrip().split(' '))
	a[i] = ai
	b[i] = bi
	d[i] = ai - bi
	i = i + 1
d.sort(reverse=True)
if sum(b) > m:
	print(-1)
elif sum(b) == m:
	print(t)
else:
	z = sum(a)
	j = 0
	while z > m:
		z = z - d[j]
		j += 1
	print(j)


l = input().split()
n = int(l[0])
m = int(l[1])
sumn = 0
sumc = 0
b = []
for i in range(n):
	temp = input().split()
	sumc = int(temp[1]) + sumc
	b.append(int(temp[0]) - int(temp[1]))
if sumc > m:
	print('-1')
else:
	b.sort()
	count = 0
	for j in range(n):
		sumc = sumc + b[j]
		if sumc <= m:
			count = count + 1
		else:
			break
	print(n - count)


(n, m) = map(int, input().split(' '))
(original, compressed) = ([], [])
for i in range(n):
	(a, b) = map(int, input().split(' '))
	original.append(a)
	compressed.append(b)
flag = True
diff = []
s = sum(original)
j = 0
res = 0
if sum(compressed) > m:
	flag = False
else:
	for i in range(n):
		diff.append(original[i] - compressed[i])
	diff.sort(reverse=True)
	while s > m:
		s -= diff[j]
		j += 1
		res += 1
if flag:
	print(res)
else:
	print(-1)


def main():
	(n, m) = [int(x) for x in input().split(' ')]
	pairs = [[int(x) for x in input().split(' ')] for i in range(n)]
	pairs.sort(key=lambda x: x[1] - x[0])
	size = sum([x[0] for x in pairs])
	ans = 0
	for p in pairs:
		if p[0] <= p[1] or size <= m:
			break
		size -= p[0] - p[1]
		ans += 1
	if size > m:
		print(-1)
		return
	print(ans)
main()


def findMin(l, n, m, sum):
	sorted = []
	n1 = n
	if sum <= m:
		return 0
	for i in range(n):
		sorted.append(l[i][0] - l[i][1])
	sorted.sort()
	sorted = sorted[::-1]
	diff = sum - m
	sum1 = 0
	for i in range(n):
		sum1 += sorted[i]
		if sum1 >= diff:
			return i + 1
	return -1
(n, m) = map(int, input().split())
k = []
sum = 0
for i in range(n):
	l = []
	l = [int(i) for i in input().split()]
	k.append(l)
	sum += l[0]
print(findMin(k, n, m, sum))


(n, m) = list(map(int, input().split()))
initial = [[0, 0] for i in range(n)]
diff = []
sumInitial = 0
for i in range(n):
	(a, b) = list(map(int, input().split()))
	temp = a - b
	diff.append(temp)
	sumInitial += a
diff.sort(reverse=True)
count = 0
while sumInitial > m:
	if count < n:
		sumInitial -= diff[count]
		count += 1
	else:
		break
if sumInitial > m:
	print('-1')
else:
	print(count)


(n, m) = [int(i) for i in input().split()]
diff = []
s = 0
for i in range(n):
	(a, b) = [int(j) for j in input().split()]
	diff.append(a - b)
	s += a
diff.sort()
ans = 0
for i in reversed(range(n)):
	if s <= m:
		break
	s -= diff[i]
	ans += 1
if s <= m:
	print(ans)
else:
	print(-1)


(n, m) = map(int, input().split())
a = [0] * n
for i in range(n):
	(x, y) = map(int, input().split())
	a[i] = (x, y)
summ = 0
for i in a:
	summ += i[0]
if summ <= m:
	print(0)
else:
	rem = summ - m
	diff = []
	for i in a:
		diff.append(i[0] - i[1])
	diff.sort(reverse=True)
	ans = 0
	curr = 0
	f = 0
	for i in range(n):
		curr += diff[i]
		ans += 1
		if curr >= rem:
			f = 1
			break
	if f == 0:
		print(-1)
	else:
		print(ans)


(n, m) = list(map(int, input().split()))
L = []
total = 0
for i in range(n):
	(a, b) = list(map(int, input().split()))
	total += a
	L += [a - b]
L.sort()

def solve(total):
	ans = 0
	while total > m and len(L) > 0:
		ans += 1
		total -= L[-1]
		L.pop()
	if total > m:
		ans = -1
	return ans
print(solve(total))


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
a = []
p = 0
for j in range(n):
	(x, y) = map(int, input().split())
	a.append(x - y)
	p += x
a.sort()
j = n - 1
q = 0
while j >= 0:
	if p <= m:
		break
	else:
		p += -a[j]
		q += 1
	j += -1
if p <= m:
	print(q)
else:
	print(-1)


def prog():
	from sys import stdin
	(n, m) = map(int, stdin.readline().split())
	mas = []
	x = 0
	from bisect import insort
	for i in range(n):
		(a, b) = map(int, stdin.readline().split())
		mas.append(a - b)
		x += a
	res = 0
	from sys import exit
	if x <= m:
		print(0)
	else:
		for (i, y) in enumerate(reversed(sorted(mas))):
			x -= y
			if x <= m:
				print(i + 1)
				exit()
		print(-1)
prog()


from bisect import bisect_left
(n, m) = map(int, input().split())
a_sum = 0
gap = []
gap_sum = 0
for _ in range(n):
	(a, b) = map(int, input().split())
	a_sum += a
	gap.append(a - b)
	gap_sum += a - b
if a_sum - gap_sum > m:
	print(-1)
elif a_sum <= m:
	print(0)
else:
	gap = sorted(gap, reverse=True)
	gap_cum = [gap[0]]
	for i in range(1, n):
		gap_cum.append(gap_cum[-1] + gap[i])
	print(bisect_left(gap_cum, a_sum - m) + 1)


import sys

def solve(songs, hard_drive_size):
	songs = sorted(songs, key=lambda x: x[2])
	total = sum([x[0] for x in songs])
	count = 0
	while total > hard_drive_size:
		count += 1
		song = songs.pop()
		total -= song[0]
		total += song[1]
	return count

def main():
	s = input().split(' ')
	n_songs = int(s[0])
	hard_drive_size = int(s[1])
	songs = []
	fully_compressed_size = 0
	for n in range(n_songs):
		s = input().split(' ')
		song_size = int(s[0])
		compressed_size = int(s[1])
		savings = abs(song_size - compressed_size)
		songs.append((song_size, compressed_size, savings))
		fully_compressed_size += compressed_size
	if fully_compressed_size > hard_drive_size:
		print(-1)
	else:
		print(solve(songs, hard_drive_size))
main()


(n, m) = map(int, input().split())
inf = 0
szhat = [0] * n
for i in range(n):
	(a, b) = map(int, input().split())
	inf += a
	szhat[i] = a - b
szhat.sort()
p = [0] * (n + 1)
for i in range(1, n + 1):
	p[i] = p[i - 1] + szhat[-i]
if inf - p[n] > m:
	print(-1)
else:
	(L, R) = (-1, n)
	while R - L > 1:
		med = (R + L) // 2
		if inf - p[med] > m:
			L = med
		else:
			R = med
	print(R)


(n, m) = map(int, input().split())
A = [0] * n
s = 0
for i in range(n):
	(a, b) = map(int, input().split())
	A[i] = a - b
	s += a
A.sort()
ans = 0
while len(A) > 0 and s > m:
	s -= A[-1]
	A.pop()
	ans += 1
if s > m:
	print(-1)
else:
	print(ans)


(n, money) = map(int, input().split())
ans = []
for _ in range(n):
	(a, b) = map(int, input().split())
	ans.append([a, b, a - b])
s = 0
for i in range(len(ans)):
	s = s + ans[i][0]
m = []
for i in range(len(ans)):
	m.append(ans[i][2])
m.sort(reverse=True)
if s <= money:
	print(0)
elif s - sum(m) > money:
	print(-1)
else:
	c = 0
	for i in range(len(m)):
		c += 1
		s = s - m[i]
		if s <= money:
			break
	print(c)


(n, m) = map(int, input().split())
sum1 = 0
main = []
for i in range(n):
	(a, b) = map(int, input().split())
	sum1 += a
	main.append(a - b)
main.sort(reverse=True)
if sum1 <= m:
	print('0')
else:
	count = 0
	f = 0
	for i in range(len(main)):
		sum1 -= main[i]
		count += 1
		if sum1 <= m:
			f = 1
			break
	if f == 0:
		print('-1')
	else:
		print(count)


(n, m) = [int(x) for x in input().split()]
li1 = []
li2 = []
for i in range(n):
	(a, b) = input().split()
	li1.append(int(a))
	li2.append(int(b))
minus = []
for i in range(n):
	minus.append(li1[i] - li2[i])
minus.sort(reverse=True)
for i in range(1, n):
	minus[i] = minus[i] + minus[i - 1]
if sum(li1) <= m:
	print(0)
	quit()
elif sum(li2) > m:
	print(-1)
	quit()
else:
	z = sum(li1)
	for i in range(n):
		if z - minus[i] <= m:
			print(i + 1)
			quit()


def skey(el):
	return el[0] - el[1]
(n, m) = map(int, input().split())
a = [''] * n
summa = 0
k = n
for i in range(n):
	a[i] = [int(j) for j in input().split()]
	summa += a[i][1]
a.sort(key=skey)
if summa > m:
	print('-1')
else:
	for i in range(n):
		summa += -a[i][1] + a[i][0]
		k -= 1
		if summa > m:
			print(k + 1)
			break
	else:
		print('0')


(n, m) = list(map(int, input().split()))
a = [0] * n
b = [0] * n
c = []
for i in range(n):
	(a[i], b[i]) = list(map(int, input().split()))
	d = a[i] - b[i]
	c.append(d)
if sum(a) <= m:
	print(0)
elif sum(b) > m:
	print(-1)
else:
	req = sum(a) - m
	c = sorted(c)[::-1]
	for i in range(n):
		if req - c[i] <= 0:
			print(i + 1)
			break
		req = req - c[i]


(n, m) = input().strip().split(' ')
(n, m) = [int(n), int(m)]
orig = []
comp = []
while n:
	(a, b) = input().strip().split(' ')
	(a, b) = [int(a), int(b)]
	orig.append(a)
	comp.append(b)
	n -= 1
if sum(orig) <= m:
	print('0')
elif sum(comp) > m:
	print('-1')
else:
	diff = []
	for x in range(0, len(orig)):
		diff.append(orig[x] - comp[x])
	diff.sort()
	c = len(diff) - 1
	sum_songs = sum(orig)
	ans = 0
	while sum_songs > m:
		sum_songs -= diff[c]
		c -= 1
		ans += 1
	print(ans)


def solve(songs, lim):
	compressed_sum = sum([song[1] for song in songs])
	if compressed_sum > lim:
		return -1
	if compressed_sum == lim:
		return len(songs)
	uncompressed_sum = sum([song[0] for song in songs])
	songs.sort(key=lambda song: song[0] - song[1], reverse=True)
	ind = 0
	cnt = 0
	while uncompressed_sum > lim:
		cnt += 1
		uncompressed_sum -= songs[ind][0]
		uncompressed_sum += songs[ind][1]
		ind += 1
	return cnt
(n, lim) = map(int, input().split())
songs = [list(map(int, input().split())) for _ in range(n)]
print(solve(songs, lim))


def solve(m, diff, p, n):
	s = p - m
	i = n - 1
	ans = 0
	while s > 0 and i >= 0:
		s -= diff[i]
		ans += 1
		i -= 1
	return ans
(n, m) = map(int, input().split())
diff = []
a = []
b = []
for i in range(n):
	(x, y) = map(int, input().split())
	diff.append(x - y)
	a.append(x)
	b.append(y)
p = sum(a)
if p <= m:
	print(0)
elif sum(b) > m:
	print(-1)
else:
	print(solve(m, sorted(diff), p, n))


(nos, needed) = [int(x) for x in input().split()]
diff = []
have = 0
for i in range(nos):
	(a, b) = [int(x) for x in input().split()]
	have += a
	diff.append(a - b)
diffs = sorted(diff, reverse=True)
for i in range(nos):
	if have <= needed:
		print(i)
		exit()
	have -= diffs[i]
if have <= needed:
	print(nos)
else:
	print(-1)


R = lambda : map(int, input().split())
(n, m) = R()
a = [[*R()] for _ in [0] * n]
s = sum((x[0] for x in a))
b = sorted((x - y for (x, y) in a))
i = 0
while s > m:
	i += 1
	if i > n:
		break
	s -= b[-i]
print((i, -1)[s > m])


(n, m) = map(int, input().split())
a = []
b = []
c = []
for i in range(n):
	(r, l) = map(int, input().split())
	a.append(r)
	b.append(l)
	c.append(r - l)
if sum(b) > m:
	print(-1)
elif sum(a) < m:
	print(0)
else:
	t = sum(a) - m
	c.sort()
	c.reverse()
	i = 0
	while t > 0:
		t -= c[i]
		i += 1
	print(i)


(a, b) = map(int, input().split())
f = []
e = []
for i in range(a):
	(c, d) = map(int, input().split())
	f.append(c - d)
	e.append(c)
f = sorted(f)[::-1]
if sum(e) > b:
	a = sum(e)
	for index in range(len(f)):
		a = a - f[index]
		if b >= a:
			print(index + 1)
			break
		elif index == len(f) - 1:
			print(-1)
else:
	print(0)


nm = input().split(' ')
n = int(nm[0])
m = int(nm[1])
tot = 0
diffs = []
for i in range(n):
	ab = input().split(' ')
	a = int(ab[0])
	b = int(ab[1])
	tot += a
	diffs.append(a - b)
diffs.sort(reverse=True)
ans = 0
i = 0
while i < len(diffs) and tot > m:
	ans += 1
	tot -= diffs[i]
	i += 1
if tot > m:
	print('-1')
else:
	print(str(ans))


(n, m) = list(map(int, input().split()))
(a, b) = ([], [])
s = 0
cs = 0
for nn in range(n):
	(aa, bb) = list(map(int, input().split()))
	a.append(aa)
	b.append(bb)
	s += aa
	cs += bb
if s <= m:
	print(0)
elif cs > m:
	print(-1)
else:
	arr = []
	for i in range(len(a)):
		arr.append(a[i] - b[i])
	res = 0
	arr.sort(reverse=True)
	t = 0
	for i in range(len(arr)):
		t += arr[i]
		res += 1
		if s - t <= m:
			break
	print(res)


l1 = []
l2 = []
ch = input()
l = list(map(int, ch.split()))
(n, m) = (l[0], l[1])
for i in range(n):
	ch = input()
	l = list(map(int, ch.split()))
	l1.append((l[0], l[1]))
l1.sort()
s = 0
s1 = 0
s2 = 0
for i in range(n):
	s += l1[i][1]
	s1 += l1[i][0]
if s > m:
	print(-1)
elif s1 <= m:
	print(0)
else:
	s2 = s1 - m
	k = 0
	h = 0
	j = n - 1
	for i in range(n):
		l2.append(l1[i][0] - l1[i][1])
	l2.sort()
	while j >= 0 and k < s2:
		k += l2[j]
		j -= 1
		h += 1
	print(h)


from operator import itemgetter
(n, m) = list(map(int, input().split()))
l = [0] * n
counter = 0
d = 0
for i in range(n):
	l[i] = list(map(int, input().split()))
for i in range(n):
	l[i][1] = l[i][0] - l[i][1]
l.sort(key=itemgetter(1), reverse=True)
for i in range(n):
	counter += l[i][0]
while counter > m:
	if d == n:
		print(-1)
		exit()
	counter -= l[d][1]
	d += 1
print(d)


line = input().split()
n = int(line[0])
SpaceAvailable = int(line[1])
actual = []
compressed = []
difference = []
for i in range(n):
	line = input().split()
	actual.append(int(line[0]))
	compressed.append(int(line[1]))
	difference.append(actual[i] - compressed[i])

def sort():
	bSorted = True
	for i in range(n - 1):
		if difference[i] < difference[i + 1]:
			temp = difference[i]
			difference[i] = difference[i + 1]
			difference[i + 1] = temp
			bSorted = False
			temp = actual[i]
			actual[i] = actual[i + 1]
			actual[i + 1] = temp
			temp = compressed[i]
			compressed[i] = compressed[i + 1]
			compressed[i + 1] = temp
	if not bSorted:
		sort()
	return
difference.sort(reverse=True)
TotalActual = 0
TotalCompressed = 0
for i in range(n):
	TotalActual += actual[i]
	TotalCompressed += compressed[i]
if SpaceAvailable < TotalCompressed:
	print(-1)
elif SpaceAvailable == TotalCompressed:
	print(n)
else:
	iCounter = 0
	while TotalActual > SpaceAvailable:
		TotalActual -= difference[iCounter]
		iCounter += 1
	print(iCounter)


(n, m) = [int(s) for s in input().split()]
red = []
hi = 0
lo = 0
for i in range(n):
	(a, b) = [int(s) for s in input().split()]
	hi += a
	lo += b
	red.append(a - b)
if hi <= m:
	print(0)
elif lo > m:
	print(-1)
else:
	red.sort(reverse=True)
	su = hi
	j = 0
	while su > m:
		su -= red[j]
		j += 1
	print(j)


(n, m) = map(int, input().split())
sf = []
ss = []
d = []
for i in range(n):
	(a, b) = map(int, input().split())
	sf.append(a)
	ss.append(b)
	d.append(a - b)
sss = sum(ss)
if sss > m:
	print(-1)
else:
	t = sum(sf)
	d.sort(reverse=True)
	i = 0
	while t > m:
		t -= d[i]
		i += 1
	print(i)


a = [int(x) for x in input().split()]
n = a[0]
m = a[1]
a = []
b = []
c = []
for i in range(n):
	d = [int(x) for x in input().split()]
	a.append(d[0])
	b.append(d[1])
	c.append(d[0] - d[1])
c.sort(reverse=True)
w = sum(a) - m
i = 0
while w > 0 and i < n:
	w -= c[i]
	i += 1
if i == n and w > 0:
	print(-1)
else:
	print(i)


(n, m) = map(int, input().strip().split())
a = []
b = []
diff = []
for i in range(n):
	(r, s) = map(int, input().strip().split())
	a.append(r)
	b.append(s)
	diff.append(r - s)
d = sum(a)
e = sum(b)
if d <= m:
	print(0)
	exit(0)
if e > m:
	print(-1)
	exit(0)
p = d - m
diff.sort()
for i in range(n - 1, -1, -1):
	p = p - diff[i]
	if p <= 0:
		print(n - i)
		exit(0)


from sys import stdin
input = stdin.readline

def main():
	(n, m) = map(int, input().split())
	a = [0] * n
	b = [0] * n
	for i in range(n):
		(a[i], b[i]) = map(int, input().split())
	c = sorted([a[i] - b[i] for i in range(n)], reverse=True)
	total = sum(a)
	pos = 0
	while pos < n and total > m:
		total -= c[pos]
		pos += 1
	if total > m:
		print(-1)
	else:
		print(pos)
main()


def go():
	(n, m) = [int(i) for i in input().split(' ')]
	s_a = 0
	s_b = 0
	c = []
	for i in range(n):
		data = [int(i) for i in input().split(' ')]
		s_a += data[0]
		s_b += data[1]
		c.append(data[0] - data[1])
	if s_b > m:
		return -1
	x = s_a
	i = 0
	c = list(sorted(c, reverse=True))
	while x > m:
		x -= c[i]
		i += 1
	return i
print(go())


(n, m) = map(int, input().split(' '))
a = []
b = []
for _ in range(n):
	(t1, t2) = map(int, input().strip().split(' '))
	a.append(t1)
	b.append(t2)
diff = []
totsize = sum(a)
for i in range(len(a)):
	diff.append(a[i] - b[i])
diff.sort()
diff.reverse()
diffpre = [0]
for i in range(len(diff)):
	diffpre.append(diffpre[-1] + diff[i])
if totsize - diffpre[-1] > m:
	print(-1)
elif totsize <= m:
	print(0)
else:
	ctr = 0
	for i in diffpre:
		if totsize - i <= m:
			break
		ctr += 1
	print(ctr)


(n, m) = map(int, input().split())
c = []
s = 0
for i in range(n):
	(a, b) = map(int, input().split())
	c.append(b - a)
	s += a
c.sort()
s = s - m
ans = 0
index = 0
while s > 0 and index < n:
	s -= -c[index]
	index += 1
if s > 0:
	print(-1)
else:
	print(index)


(n, m) = [int(x) for x in input().split(' ')]
nx = []
for i in range(n):
	(a, b) = [int(x) for x in input().split(' ')]
	nx.append((a, b))
nb = sum([x[0] for x in nx])
if nb <= m:
	print(0)
elif sum([x[1] for x in nx]) > m:
	print(-1)
else:
	needed = nb - m
	nx.sort(key=lambda k: k[1] - k[0])
	saved = 0
	for (i, nv) in enumerate(nx):
		saved += nv[0] - nv[1]
		if saved >= needed:
			print(i + 1)
			break


import sys
(n, m) = map(int, input().split())
song = [None] * n
for i in range(n):
	song[i] = list(map(int, input().split()))
Sum = 0
Minus = [None] * n
for i in range(n):
	Sum += song[i][0]
	Minus[i] = song[i][0] - song[i][1]
Minus.sort(reverse=True)
for j in range(n):
	if Sum <= m:
		print(j)
		sys.exit()
	else:
		Sum = Sum - Minus[j]
if Sum <= m:
	print(j + 1)
	sys.exit()
print(-1)


(n, m) = map(int, input().split())
rec = []
sa = 0
sb = 0
for i in range(n):
	(a, b) = map(int, input().split())
	sa += a
	sb += b
	rec.append(a - b)
rec = sorted(rec, reverse=True)
if sb > m:
	print(-1)
elif sa < m:
	print(0)
else:
	i = 0
	while sa > m:
		sa -= rec[i]
		i += 1
	print(i)


(n, m) = [int(i) for i in input().split()]
(un, s) = (0, 0)
diff = []
for _ in range(n):
	(a, b) = [int(i) for i in input().split()]
	diff.append(a - b)
	un += a
	s += b
if un <= m:
	print('0')
elif s > m:
	print('-1')
else:
	diff.sort(reverse=True)
	count = 0
	for i in range(n):
		un = un - diff[i]
		count += 1
		if un <= m:
			break
	print(count)


import heapq

def main():
	h = []
	(n, m) = map(int, input().split())
	need = -m
	for _ in range(n):
		(a, b) = map(int, input().split())
		need += a
		heapq.heappush(h, b - a)
	try:
		res = 0
		while need > 0:
			need += heapq.heappop(h)
			res += 1
	except IndexError:
		res = -1
	print(res)
main()


(n, m) = map(int, input().split())
data = [list(map(int, input().split())) for i in range(n)]
min_size = 0
for i in range(n):
	min_size += data[i][1]
if min_size > m:
	print(-1)
else:
	size = 0
	for i in range(n):
		size += data[i][0]
	compression = [data[i][0] - data[i][1] for i in range(n)]
	compression.sort()
	i = n - 1
	ans = 0
	while size > m:
		size -= compression[i]
		i -= 1
		ans += 1
	print(ans)


(n, capacity) = map(int, input().split())
u_size = c_size = u_song = c_song = c = 0
compress = []
for i in range(n):
	(u_size, c_size) = map(int, input().split())
	compress.append(u_size - c_size)
	u_song += u_size
	c_song += c_size
if c_song > capacity:
	print(-1)
else:
	compress.sort(reverse=True)
	while u_song > capacity:
		u_song -= compress[c]
		c += 1
	print(c)
	exit(0)


import itertools
(n, m) = map(int, input().split())
(a, b) = zip(*[tuple(map(int, input().split())) for _ in range(n)])
total_diff = sum(a) - m
res = 0
if total_diff > 0:
	d = sorted([f[0] - f[1] for f in zip(a, b)], reverse=True)
	acc = list(map(lambda x: x >= total_diff, itertools.accumulate(d)))
	res = -1 if not acc[-1] else acc.index(True) + 1
print(res)


from bisect import bisect_left
from itertools import accumulate
from sys import stdin
all_in = stdin.readlines()
(n, m) = map(int, all_in[0].split())
p = [tuple(map(int, el.split())) for el in all_in[1:]]
a = [el[0] for el in p]
b = [el[1] for el in p]
c = [a[i] - b[i] for i in range(n)]
s = sum(a)
if s <= m:
	print(0)
	exit()
k = s - m
c.sort(reverse=True)
acc = list(accumulate(c))
b = bisect_left(acc, k)
if b == n:
	print(-1)
	exit()
print(b + 1)


(n, m) = list(map(int, input().split()))
a = []
s = 0
for i in range(n):
	(A, B) = list(map(int, input().split()))
	a.append(A - B)
	s += A
a.sort()
ans = 0
if s <= m:
	print(ans)
	exit()
for i in range(n - 1, -1, -1):
	s -= a[i]
	ans += 1
	if s <= m:
		print(ans)
		break
else:
	print(-1)


(n, m) = map(int, input().split())
arr = []
for i in range(n):
	(a, b) = map(int, input().split())
	arr.append([a, b, a - b])
presum = 0
for i in range(n):
	presum += arr[i][0]
p1 = 0
for i in range(n):
	p1 += arr[i][1]
if p1 > m:
	print('-1')
else:
	arr = sorted(arr, key=lambda item: item[2])
	arr = arr[::-1]
	c = 0
	for i in range(n):
		if presum <= m:
			break
		else:
			c += 1
			presum -= arr[i][0] - arr[i][1]
	print(c)


import sys
import math
import collections
import bisect

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
for t in range(1):
	(n, m) = get_ints()
	space = 0
	saved = []
	for i in range(n):
		(a, b) = get_ints()
		space += a
		saved.append(a - b)
	saved.sort(reverse=True)
	if space - sum(saved) > m:
		print(-1)
		continue
	i = 0
	count = 0
	if space <= m:
		print(0)
		continue
	while i < n:
		count += 1
		space -= saved[i]
		if space <= m:
			print(count)
			break
		i += 1


import re
import sys
exit = sys.exit
from bisect import bisect_left as bsl, bisect_right as bsr
from collections import Counter, defaultdict as ddict, deque
from functools import lru_cache
cache = lru_cache(None)
from heapq import *
from itertools import *
from math import inf
from pprint import pprint as pp
enum = enumerate
ri = lambda : int(rln())
ris = lambda : list(map(int, rfs()))
rln = sys.stdin.readline
rl = lambda : rln().rstrip('\n')
rfs = lambda : rln().split()
mod = 1000000007
d4 = [(0, -1), (1, 0), (0, 1), (-1, 0)]
d8 = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
(n, m) = ris()
a = []
s = 0
for _ in range(n):
	(x, y) = ris()
	a.append((x, y))
	s += y
if s > m:
	print(-1)
	exit()
a.sort(key=lambda x: x[0] - x[1])
ans = n
for (x, y) in a:
	s -= y
	s += x
	if s > m:
		break
	ans -= 1
print(ans)


(n, m) = map(int, input().split())
a = [0] * n
b = [0] * n
raz = [0] * n
for i in range(n):
	(x, y) = map(int, input().split())
	a[i] = x
	b[i] = y
	raz[i] = x - y
if sum(b) > m:
	print(-1)
	exit()
raz.sort()
c = sum(a)
if c <= m:
	print(0)
	exit()
raz.reverse()
for i in range(n):
	c -= raz[i]
	if c <= m:
		print(i + 1)
		exit()


from sys import stdin, stdout
import bisect
import math
from collections import Counter

def st():
	return list(stdin.readline().strip())

def inp():
	return int(stdin.readline())

def li():
	return list(map(int, stdin.readline().split()))

def mp():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def soe(limit):
	l = [1] * (limit + 1)
	prime = []
	for i in range(2, limit + 1):
		if l[i]:
			for j in range(i * i, limit + 1, i):
				l[j] = 0
	for i in range(2, limit + 1):
		if l[i]:
			prime.append(i)
	return prime

def segsoe(low, high):
	limit = int(high ** 0.5) + 1
	prime = soe(limit)
	n = high - low + 1
	l = [0] * (n + 1)
	for i in range(len(prime)):
		lowlimit = low // prime[i] * prime[i]
		if lowlimit < low:
			lowlimit += prime[i]
		if lowlimit == prime[i]:
			lowlimit += prime[i]
		for j in range(lowlimit, high + 1, prime[i]):
			l[j - low] = 1
	for i in range(low, high + 1):
		if not l[i - low]:
			if i != 1:
				print(i)

def power(a, n):
	r = 1
	while n:
		if n & 1:
			r = r * a
			n = n - 1
		else:
			a = a * a
			n = n >> 1
	return r

def solve():
	(n, m) = mp()
	z = []
	(x, y) = (0, 0)
	for i in range(n):
		(a, b) = mp()
		x += a
		y += b
		z.append(a - b)
	if y > m:
		pr(-1)
	else:
		o = x - m
		z.sort(reverse=True)
		i = 0
		c = 0
		while x > m:
			x -= z[i]
			i += 1
			c += 1
		pr(c)
for _ in range(1):
	solve()


def i_ints():
	return map(int, input().split())

def main():
	(n, m) = i_ints()
	total = 0
	deltas = []
	for i in range(n):
		(a, b) = i_ints()
		total += a
		deltas.append(a - b)
	if total <= m:
		print(0)
		return
	deltas.sort(reverse=True)
	from itertools import accumulate
	cum_deltas = list(accumulate(deltas))
	from bisect import bisect_left
	p = bisect_left(cum_deltas, total - m)
	if p >= len(deltas):
		print(-1)
		return
	print(p + 1)
main()


(n, m) = input().split()
(n, m) = (int(n), int(m))
a = [0] * n
b = []
count = 0
for i in range(int(n)):
	count += 1
	temp = list(map(int, input().split()))
	t = int(temp[1]) - int(temp[0])
	a[i] = t
	b.append(temp[0])
a = sorted(a)
result = 0
temp_a = sum(a)
temp_b = sum(b)
temp = temp_b
if temp_a + temp_b > m:
	print(-1)
else:
	for j in range(len(a)):
		if temp > m:
			temp += a[j]
			result += 1
	print(result)


(n, m) = map(int, input().split())
L = []
sm = 0
for i in range(n):
	(a, b) = map(int, input().split())
	sm += a
	L.append(a - b)
L.sort()
L.reverse()
ans = 0
if sm > m:
	for x in L:
		sm -= x
		ans += 1
		if sm <= m:
			break
if sm <= m:
	print(ans)
else:
	print(-1)


(n, m) = map(int, input().split())
l = []
k = []
s1 = 0
s2 = 0
for i in range(n):
	(a, b) = map(int, input().split())
	s1 += a
	s2 += b
	l.append(a - b)
if s2 > m:
	print(-1)
else:
	o = 0
	l.sort()
	while s1 > m:
		o += 1
		s1 -= l.pop()
	print(o)


(n, m) = map(int, input().split())
a = []
s = 0
for i in range(n):
	(ai, bi) = map(int, input().split())
	s += ai
	a.append(abs(ai - bi))
a.sort(key=lambda x: -x)
i = 0
while s > m:
	if i >= len(a):
		print(-1)
		exit(0)
	s -= a[i]
	i += 1
print(i)


(n, m) = input().strip().split(' ')
n = int(n)
m = int(m)
songs = []
total_size = 0
for i in range(n):
	(a, b) = input().strip().split(' ')
	a = int(a)
	b = int(b)
	songs.append([a, b, a - b])
	total_size += a
songs = sorted(songs, key=lambda song: song[2])
num_compressed = 0
while num_compressed < n and total_size > m:
	total_size -= songs[-num_compressed - 1][2]
	num_compressed += 1
if total_size > m:
	print(-1)
else:
	print(num_compressed)