import sys
def read_n_integers(n: int) -> list[int]:
"""Reads stream inputs until exactly `n` integers are collected."""
numbers = []
# Reads tokens continuously from standard input
for line in sys.stdin:
numbers.extend(map(int, line.split()))
if len(numbers) >= n:
break
return numbers[:n]
def match_pairs(wants_taller: list[int], wants_shorter: list[int]) -> int:
"""Greedily pairs individuals who want taller partners with those who want shorter partners.
wants_taller: Heights of people needing a taller partner.
wants_shorter: Heights of people needing a shorter partner.
"""
wants_taller.sort()
wants_shorter.sort()
pairs_count = 0
taller_idx = 0
shorter_idx = 0
while taller_idx < len(wants_taller) and shorter_idx < len(wants_shorter):
# Check if the person expecting a shorter partner is taller than the required height
if wants_shorter[shorter_idx] > wants_taller[taller_idx]:
pairs_count += 1
taller_idx += 1
shorter_idx += 1
else:
# Person wanting a shorter partner is too short; test the next taller candidate
shorter_idx += 1
return pairs_count
def main():
input_data = sys.stdin.read().split()
if not input_data:
return
n = int(input_data[0])
# Extract all elements for group 1 (men) and group 2 (women)
arr1 = [int(x) for x in input_data[1 : n + 1]]
arr2 = [int(x) for x in input_data[n + 1 : 2 * n + 1]]
# Categorize Men
men_want_taller = [x for x in arr1 if x > 0]
men_want_shorter = [abs(x) for x in arr1 if x < 0]
# Categorize Women
women_want_taller = [x for x in arr2 if x > 0]
women_want_shorter = [abs(x) for x in arr2 if x < 0]
# Calculate valid pairs for both compatible configurations
total_matches = match_pairs(
women_want_taller, men_want_shorter
) + match_pairs(men_want_taller, women_want_shorter)
print(total_matches)
if __name__ == "__main__":
main()