Phone Conversations
Based on the examination results, patients at the local psychiatric hospital (LPH) are noticeably calmer when they engage in communication with each other. However, for safety reasons, direct communication is not permitted. Therefore, it has been decided that patients will communicate through phone conversations.
At the LPH, patients are organized into small groups. For each group, another group has been identified as the most suitable for conversation. A patient from group (A) can converse with a patient from group (B) if and only if group (A) is suitable for group (B), or vice versa. The LPH management is eager to purchase the necessary equipment for this setup. To do so, they need to determine the maximum number of patient pairs that can be formed for conversations, ensuring each patient is part of only one pair. You have been tasked with finding this number.
Input
The first line contains an integer (N) ((1 N 10000)) representing the number of patient groups in the LPH. This is followed by (N) lines, each describing a group. Each group is defined by two numbers, (N_i) and (P_i) ((1 N_i 10), (1 P_i N)), where (N_i) is the number of patients in the group, and (P_i) is the number of the group deemed suitable for conversation with the current group (a group can be suitable for itself). Groups are numbered from (1) to (N) and are listed in ascending order.
Output
Output a single number representing the maximum number of patient pairs that can be formed for conversations.