The Little Elephant loves to play with color cards.
He has n cards, each has exactly two colors (the color of the front side and the color of the back side). Initially, all the cards lay on the table with the front side up. In one move the Little Elephant can turn any card to the other side. The Little Elephant thinks that a set of cards on the table is funny if at least half of the cards have the same color (for each card the color of the upper side is considered).
Help the Little Elephant to find the minimum number of moves needed to make the set of n cards funny.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of the cards. The following n lines contain the description of all cards, one card per line. The cards are described by a pair of positive integers not exceeding 109 — colors of both sides. The first number in a line is the color of the front of the card, the second one — of the back. The color of the front of the card may coincide with the color of the back of the card.
The numbers in the lines are separated by single spaces.
Output
On a single line print a single integer — the sought minimum number of moves. If it is impossible to make the set funny, print -1.
Examples
Input
3
4 7
4 7
7 4
Output
0
Input
5
4 7
7 4
2 11
9 7
1 1
Output
2
Note
In the first sample there initially are three cards lying with colors 4, 4, 7. Since two of the three cards are of the same color 4, you do not need to change anything, so the answer is 0.
In the second sample, you can turn the first and the fourth cards. After that three of the five cards will be of color 7.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve: Given n cards, each has front color a_i, back color b_i. Initially all front up. We can flip any subset of cards (each flip toggles side). After flips, we consider the color on top for each card. We want to achieve a state where there exists a color C such that at least ceil(n/2) cards have color C? Wait "at least half of the cards have the same color". Usually "at least half" means >= n/2. But careful: "at least half of the cards have the same color (for each card the color of the upper side is considered)." Usually that means: there exists a color such that the number of cards showing that color is >= ceil(n/2)? But the phrasing "at least half" could be interpreted as >= n/2. Since n can be odd, half of odd is not integer, so "at least half" means >= n/2 (which for odd is > floor(n/2) but not integer). Typically, for odd n, condition is >= (n+1)/2, because at least half meaning more than half? But "at least half" literally means >= n/2. For odd n, n/2 is not integer, so we need to consider the condition: number of cards >= n/2. Since n/2 is not integer, the integer condition is > n/2? Actually, if n=5, half is 2.5. At least half means >=2.5, so integer must be >=3. So yes, it's ceil(n/2). Many problems use "at least half" meaning >= n/2 and for odd n it's the smallest integer >= n/2, which is (n+1)/2. So it's ceil(n/2). We'll adopt that.
Goal: minimize number of flips to achieve existence of color C with count >= ceil(n/2). Flipping a card changes the color from front to back.
If impossible, print -1.
Constraints: n up to 1e5, colors up to 1e9.
We need to compute min flips.
Observation: Since we only care about achieving a majority color (>= ceil(n/2) cards showing that color). For each possible color C that could become the majority, we can consider the minimal flips needed to have at least ceil(n/2) cards showing C. Then take min over all colors. If none achievable, answer -1.
But colors are many (up to 2n unique colors). So we need to process candidate colors efficiently. Only colors that appear on some card could be candidate. Actually, a color that never appears on any side cannot become majority. So candidate set = all colors that appear in either side of any card. At most 2n unique colors.
Now for a given color C, we need to compute minimal flips to have at least H = ceil(n/2) cards showing C.
For each card i, we have two colors: a_i (front), b_i (back). Initially shows a_i. Flipping changes to b_i. So for a given target C, each card i can contribute in one of the following ways:
If a_i == C and b_i == C: both sides are C. Then no matter what, the card will show C. Flipping or not doesn't matter, always shows C. So it's a fixed C.
If a_i == C but b_i != C: then initially shows C, flipping makes it not C (since b_i != C). So to have it show C, we must NOT flip it. So it contributes 0 flips if we leave it.
If a_i != C but b_i == C: then initially not C, flipping makes it C. So to have it show C, we must flip it (cost 1).
If a_i != C and b_i != C: then card can never show C. So it must count as not C.
But also note if a_i == C and b_i == C: it's always C, cost 0 either way.
Thus, for each card we have a status relative to C: either it can be forced to be C at some cost, or it can't be C.
Define for each card i:
If a_i == C and b_i == C: type A = always C, cost 0, can count as C regardless of flips.
If a_i == C and b_i != C: type B = initially C, flipping loses it. So to keep C, must not flip. So cost 0 if we want C, but flipping would lose it. However, if we flip, it becomes not C. So we could choose to not count it as C, but then we waste it. But we want to achieve at least H cards showing C. So we want to decide which cards to have show C. For type B, we can either leave it (cost 0, contributes 1) or flip (cost 1, contributes 0). But we want to minimize total flips while achieving total count >= H. So we can choose a subset of these to be contributors, paying 0 flips for those we keep as C, and for those we don't use we could flip or not? Actually if we don't need them to be C, we don't care what they show, so we could leave them as is or flip, but flipping may incur cost without benefit, so we wouldn't flip them. So for type B cards, if we don't need them to be C, we leave them (cost 0) and they show not C. So effectively each type B can contribute 1 to C count at cost 0 if we choose to count it, else contributes 0 at cost 0. But note that we may have to flip some cards from other types to achieve the count. So for type B, we might want to count them because they are free.
If a_i != C and b_i == C: type C = can be made C only by flipping, cost 1, contributes 1 if flipped, else 0.
If a_i != C and b_i != C: type D = never C, always not C, cost irrelevant.
But also there is the case where both sides are C? Already included.
So essentially, we have items that can be chosen to contribute to C, each with a cost (0 or 1) if chosen; if not chosen cost 0 (since we leave them as is). We also have items that always contribute (type A) and cannot be changed; they are always C, so they automatically count towards the C count. Let always_count = number of cards where both sides are C (or, more precisely, cards where both sides are C? Actually if both sides are C, then it's always C regardless of flip, so it's automatically a contributor. But note that if we flip it, it remains C, so it's still contributor. So they are automatically counted. However, could we possibly choose to not count them? No, they will always show C, so they always contribute. So they are forced contributors. So any target color C must have that always_count <= n obviously, but also they are automatically part of the count.
Then we have optional cards: type B and type C. For type B, they currently show C (since a_i == C). So they are contributors by default with cost 0. But if we flip them, they'd stop being contributors. But we would never flip them if we want to maximize contributors with minimal cost; we might want to flip them only if we need to reduce the count? But we need at least H, so we want more contributors, not less. So we would never flip a type B card, because flipping costs 1 and reduces the count, which is undesirable. So effectively, for achieving >= H, we can treat type B as always contributors at cost 0? Wait careful: the problem asks to minimize number of flips to achieve the condition. We can decide to flip any cards. But if we flip a type B, we incur a flip and lose a contributor. That seems counterproductive unless we have too many contributors? But the condition is at least H, not exactly H. Having more than H is fine, it still satisfies the condition. So we never need to reduce the number of contributors. So we wouldn't flip a type B. So for the purpose of achieving at least H, we can assume we never flip type B, because flipping would only increase flips and possibly reduce contributors, which is never beneficial. Therefore type B cards are effectively always contributors at zero cost. However, there might be scenario where we have more than H contributors automatically (including type A and B) without any flips, then answer 0. That's fine. So type B are automatically contributing.
But wait: what if the front color is C, back is not C, and we flip some other cards to achieve C? We don't need to flip B. So indeed, for minimizing flips, we will keep B as is because flipping would only increase flips without benefit. So B's contribution is always counted. But could there be a case where we need to flip a B to achieve something else? No, the condition is only about the majority color; we don't care about other colors. So we don't need to worry about other colors. So B's are automatic.
Now, type C: initially not C, flipping yields C at cost 1. So they are potential contributors at cost 1 each. We can choose to flip some of them to increase the count.
Type D: never C.
So total forced contributors:
Count_always = number of cards where both sides = C.
Count_frontC = number of cards where a_i == C and b_i != C.
Let forced = count_always + count_frontC. Actually frontC (type B) are forced contributors if we don't flip them. But we might flip them? As argued, no reason to flip them. So they are effectively forced contributors because we will not flip them. But is there any scenario where flipping a B could be beneficial? Possibly if we want to achieve majority for a different color? But we are considering per color C individually; for that color, we only care about achieving it as the majority. Flipping a B (which shows C) would reduce the count of C, which is detrimental. So no.
Thus for each color C, we have base = number of cards that already show C initially (i.e., with a_i == C) plus those always C? But if both sides are C, they also show C initially (since a_i == C) so they are included in frontC? Actually both sides C: a_i == C, so it's also a frontC case? We need to differentiate: if b_i == C as well, then it's both sides C. In that case, it's a special type: it's counted in frontC as well, but flipping it still shows C. So base = number of cards with a_i == C (including those where b_i may or may not be C). But note: if a_i == C, initial side up is C. So yes, the initial count of C is simply the number of cards where a_i == C. So base = count of a_i == C. That includes both type A and type B. However, type A are also always C, but that's fine.
But then the optional cards: those where a_i != C but b_i == C, i.e., backC only. Those are the ones we can flip to add to C count.
But wait: what about cards where a_i == C and b_i == C? They are already counted in base. Flipping them doesn't change anything. So they are essentially free, no need to flip.
Thus, the problem reduces: For each color C, let total_initial = freq_front[C] = number of i such that a_i == C. This is the number of cards currently showing C. Let additional_possible = number of i such that a_i != C and b_i == C. These are cards we can flip to make them show C, each costing 1 flip. The cards with a_i != C and b_i != C are irrelevant.
Goal: Achieve total showing C >= H = ceil(n/2). Initially we have total_initial. If total_initial >= H, answer = 0 for that color (no flips needed). Otherwise, we need to flip some of the additional_possible cards to increase the count. We need to flip at least need = H - total_initial cards (provided need <= additional_possible). Each flip costs 1. So minimal flips for that color = need, if need <= additional_possible. If not possible (total_initial + additional_possible < H), then this color cannot reach majority.
Thus answer = min over colors C of max(0, H - total_initial) subject to total_initial + additional_possible >= H.
But is it always optimal to only flip cards from additional_possible? Could we ever flip a card that currently shows C (a_i == C) to something else, and then flip some other card to C to achieve a better overall flip count? That seems counterintuitive: flipping a C card would reduce the count, requiring more flips to bring back count, so net flips increase. However, consider a scenario where there are cards with both sides C (type A). Flipping them doesn't change count, so no effect. For type B (a_i == C, b_i != C), flipping reduces count by 1 (and costs 1), and we could flip another card (with backC) to increase count by 1 (cost 1). That would be net flips = 2, but we could have just not touched them. So worse. What about flipping a card that shows C (type B) to not C, but maybe that allows us to achieve a different majority color? But we're considering per color C, we only care about achieving majority for that color. So flipping away a C card would only hurt.
But there is nuance: The condition is "at least half of the cards have the same color". It doesn't specify which color; we can choose any color. So for each candidate color, we compute min flips. So within that color, we can choose which cards to flip. The reasoning above seems correct: to maximize count of C with minimal flips, we should never flip a card that already shows C, because it would reduce count without need. However, consider a card where a_i == C, b_i == C (both sides C). Flipping it doesn't change count, but also doesn't cost anything? Actually flipping costs a move; flipping any card, even if it results in same color, still counts as a move. The problem statement: "In one move the Little Elephant can turn any card to the other side." If both sides are the same color, turning doesn't change the color, but it's still a move. Doing that would be wasteful, we wouldn't do it because it increases moves without benefit. So we ignore.
Thus the minimal flips for color C is simply need = max(0, H - total_initial) if we have enough additional cards to flip. However, careful: What about cards where a_i != C, but b_i == C? Yes, those are the only ones that can be flipped to gain a C. But also, what about cards where a_i != C and b_i == C? They are counted in additional_possible. Also, what about cards where a_i == C and b_i == C? They are already counted in total_initial, and flipping them doesn't change count, so they are not used for additional.
But there is another scenario: If a card has a_i != C, b_i != C, can't become C. So no.
Thus simple formula.
But wait, there might be an edge case: Suppose we have a card with a_i == C and b_i == C. It's always C. So it's forced. Good.
So algorithm:
Compute H = ceil(n/2) = (n+1)//2.
For each card i, read a_i, b_i.
Build mapping from color to:
front_count[color]: number of cards with a_i == color.
back_only_count[color]? Actually we need for each color C, the number of cards where a_i != C and b_i == C. We can compute this as: for each card, if b_i != a_i (or even if equal but we need to exclude those where a_i == C?), we can contribute to a map back_not_front[color] where we count cards where b_i == color and a_i != color. But careful: If a_i == b_i == C, then it's both front and back. That card contributes to front_count[C], and also b_i == C but a_i == C, so it would be counted in back_not_front if we just count all cards with b_i == color and a_i != color? But we need to exclude those where a_i == C because we already count them in front_count, and flipping such a card from back to front would not increase count because it's already counted in front. Actually if a_i == C and b_i == C, flipping does nothing, but if we treat it as additional_possible, we might think we can flip it to get C, but it's already C. So we must exclude cards where a_i == C from additional_possible, because they are already showing C initially. So for additional_possible for color C, we consider cards where b_i == C and a_i != C. That's correct.
Thus we need to compute for each color C:
total_initial = front_count[C] (number of cards with a_i == C).
additional = count of cards with b_i == C and a_i != C.
We could compute by iterating over cards and updating maps:
For each card (a,b):
front_count[a] += 1.
If a != b: add to a map back_map: back_count[b] maybe but careful: we need to know for each color, how many cards have b == color and a != color. So we could increment back_count_excl[b] by 1 for cards where a != b? But if a == b, then it's not additional for that color because a == C, so we should exclude. However, for a == b, b == a, but a == C, so b == C, but a == C, so it's not additional. So we should not count it in additional. So for a != b, we can increment back_excl[b] by 1, because then a != b ensures a != color (b). But wait, what if a != b, but still a might equal the color? Actually if a != b and we increment back_excl[b], we are counting the card as having b = C, and we assume a != C because a != b. But it's possible that a == some other value; but is it guaranteed that a != b implies a != C? Since C = b, if a != b, then indeed a != b, so a != C. So that's correct. So for each card with a != b, we can increment back_excl[b] by 1.
But careful: What about cards where a != b, but also a may equal C for other C? That's irrelevant; we are counting per color b. So that works.
So:
if a != b:
back_excl[b] += 1
But we also need to consider that for a color C, there might be cards with b == C but a == C (a==b case). Those should not be counted in additional. Our back_excl only counts a != b, so they are not counted, good.
Thus for each color C, additional = back_excl[C] (since only cards with b == C and a != C are counted). But is there any case where a card has a != b, b == C, but a also equals C? That's impossible because if a == C and b == C then a == b. So not counted.
Thus we can compute front_count and back_excl.
Then for each color that appears (union of keys from front_count and back_excl), compute:
base = front_count.get(C,0)
extra = back_excl.get(C,0)
total = base + extra
if total >= H:
need = max(0, H - base)
ans = min(ans, need)
But also what about colors that appear only in back_excl? Yes they are candidates.
Edge Cases: n=1. H = ceil(1/2)=1. For a single card, we need at least 1 card of same color, which is always true? Wait any color? The condition: "at least half of the cards have the same color". With one card, half is 0.5, so at least one card with same color means any single card? Actually we need at least 1 card of some color. That is always true: the card's color (whatever it is) appears at least once. So answer should be 0, because the set is already funny: the one card's color appears 1 time, which is >= 0.5, yes. So answer 0. But check our formula: H = 1. For the color that is currently showing (a1), base=1, total=1 (if back_excl may add?), need = max(0,1-1)=0. So answer 0. Good.
Now, is there any scenario where flipping a card that currently shows C (type B) could be beneficial for achieving majority of a different color D? That is captured by evaluating each color separately. So our per-color analysis stands.
But we must be careful: Our reasoning assumed that for a given color C, we only flip cards from additional set (backC only) to become C, and we never flip away C cards. However, is it ever beneficial to flip a card that currently shows C to another color, not to achieve C majority, but to achieve majority of some other color? But we are checking all colors, so if flipping away a C could help achieve majority for another color with fewer total flips, that would be considered when evaluating that other color. So our per-color approach is fine.
Thus solution seems straightforward: compute front count, back_excl count (for a != b), then for each distinct color, compute need.
Complexities: O(n) time, O(number of distinct colors) memory.
But is this always correct? Let's test with given examples.
Example 1:
3 cards:
(4,7)
(4,7)
(7,4)
n=3, H=2 (ceil(3/2)=2)
front_count: for 4: 2 (first two have a=4), for 7:1 (third has a=7)
back_excl: For each card, a != b? For first (4,7): a!=b -> back_excl[7]+=1, second same -> back_excl[7]+=1 => back_excl[7]=2, third (7,4): a!=b -> back_excl[4]+=1 => back_excl[4]=1.
Now check colors:
Color 4: base=2, extra=back_excl[4]=1, total=3 >=2, need = max(0,2-2)=0 -> ans=0.
Color 7: base=1, extra=back_excl[7]=2, total=3>=2, need = max(0,2-1)=1. So min ans = 0. Output 0. matches.
Example 2:
5 cards:
1: 4 7
2: 7 4
3: 2 11
4: 9 7
5: 1 1
n=5, H=3 (ceil5/2=3)
front_count:
4:1 (card1), 7:1 (card2), 2:1 (card3), 9:1 (card4), 1:1 (card5)
back_excl: for each if a!=b
card1: 4!=7 -> back_excl[7]+=1
card2: 7!=4 -> back_excl[4]+=1
card3: 2!=11 -> back_excl[11]+=1
card4: 9!=7 -> back_excl[7]+=1
card5: 1==1 -> skip
So back_excl: 7:2, 4:1, 11:1.
Now evaluate candidates:
Color 4: base=1, extra=1 => total=2 <3 -> not possible.
Color 7: base=1, extra=2 => total=3>=3, need = max(0,3-1)=2.
Color 2: base=1, extra=0 => total=1<3.
Color 9: base=1, extra=0 => total=1<3.
Color 1: base=1, extra=0 (since back_excl[1]? card5 not counted, also no other) => total=1<3.
Color 11: base=0, extra=1 => total=1<3.
Thus min need =2. Output 2, matches.
So algorithm works for examples.
But is there any subtlety? Let's think about scenario where flipping a card that initially shows C could reduce the needed flips for achieving majority of a different color, but then we might also need to flip some cards to that different color. That is captured by evaluating that different color. However, could there be a scenario where achieving majority for some color requires flipping some cards that currently show that color away, because flipping them might help increase count of that same color? That doesn't make sense.
Alternatively, could there be scenario where we achieve majority for a color not by flipping to that color directly, but by flipping away cards of other colors to reduce total n? No, total n is fixed. The condition is about counts of colors; flipping cards changes the displayed color, not the total number. So no.
Thus solution seems straightforward.
But wait: There is a nuance: The problem statement: "at least half of the cards have the same color". It doesn't say that the majority color must be the one we aim for; it just says there exists a color that appears on at least half of the cards. So we can pick any color. So our per-color approach is correct.
Now consider scenario where a color C appears both as front and back on many cards. Our formula: base = front_count[C] (cards with a=C). However, there could be cards with a != C and b = C (back only) which we can flip. But also there might be cards with a = C and b = C (both sides same). That is included in front_count, and also they are always C. Good.
But what about cards where a = C and b = C? Flipping them does nothing. They are counted in base. In the additional count we exclude them because a==C. Good.
Now, is there any scenario where we might flip a card with a = C and b != C (type B) to the back (non-C) and then later flip it back? No.
Thus answer is min_{C} (max(0, H - front_count[C])) provided front_count[C] + back_excl[C] >= H.
But is there any scenario where we might need to flip a card with both sides same color (a=b=C) to achieve something? It wouldn't change anything.
So that's it.
But we must also consider if the answer could be -1. That happens if for all colors, front_count[C] + back_excl[C] < H. That is, no color can be made to appear on at least H cards. That would be impossible.
Now we need to confirm with some tricky cases.
Case: n=2, H=1. Actually ceil(2/2)=1? Wait ceil(2/2)=1 because 2/2=1 exactly, so >=1. So condition is that there is a color appearing on at least 1 card. That is always true. So answer always 0. Our algorithm: For any color, base+extra may be less than 1? But base for the color showing on the first card is at least 1, so condition holds. So answer 0. Good.
Case: n=2, but both cards have a1=1, b1=2; a2=3, b2=4. front_count: 1:1, 3:1. For color 1: base=1, extra=0? back_excl[1]? b1=2 not 1, b2=4, so extra=0, total=1>=1, need=0. So answer 0. Indeed color 1 appears on one card which is half of 2? 1 >=1, okay.
Case: n=3, H=2. Suppose cards: (1,2), (2,3), (3,1). front_count: 1:1,2:1,3:1. back_excl: from (1,2): back_excl[2]+=1, (2,3): back_excl[3]+=1, (3,1): back_excl[1]+=1. So each color C has base=1, extra=1, total=2>=2, need= max(0,2-1)=1. So answer 1. Can we achieve majority with 1 flip? Let's test: Initially colors: 1,2,3. No majority. Flip one card to make two same. For instance, flip card1 (1->2): now colors:2,2,3 -> two 2's, majority. So 1 flip works. So correct.
Case: n=3, H=2, cards: (1,1), (2,2), (3,3). front_count: 1,2,3 each 1. back_excl: all a==b, so none. For each color, total =1 <2 -> impossible? But wait, can we achieve majority? Initially colors all distinct, no majority. Flipping any card doesn't change color (both sides same). So we can never get two cards of same color. So impossible, answer -1. Indeed our algorithm would find all colors total <2, so ans stays INF -> -1. Good.
Case: n=3, cards: (1,1), (1,2), (2,2). front_count: 1:2 (first and second? Actually first: (1,1) a=1; second: (1,2) a=1; third: (2,2) a=2) => front_count[1]=2, front_count[2]=1. back_excl: second: (1,2) a!=b -> back_excl[2]+=1. Others a=b. So total for color 1: base=2, extra=0 => total=2 >=2, need=0 -> answer 0. Indeed two cards show 1 initially. Good.
Case: n=3, cards: (1,2), (1,2), (3,2). front_count: 1:2, 3:1. back_excl: first two: back_excl[2]+=2; third: (3,2) a!=b -> back_excl[2]+=1 => back_excl[2]=3. Now color 2: base=0? front_count[2]=0, extra=3 => total=3>=2, need = max(0,2-0)=2. But can we achieve majority of color 2 with 2 flips? Initially colors: 1,1,3. To get majority of 2, we need at least 2 cards showing 2. Flipping first and second cards (each from 1 to 2) gives colors: 2,2,3 => majority 2 (2 out of 3). That's 2 flips. Could we do with 1 flip? Flip third card from 3 to 2 gives colors:1,1,2 -> only one 2, not majority. Flip one of first two: gives 2,1,3 -> one 2. So need 2. Good.
Color 1: base=2, extra=0 => total=2, need=0 -> answer 0? But wait, base=2 already gives majority? 2 out of 3 is >=2, so yes, answer 0. So final answer is 0. That matches condition initially: two 1's already. So min is 0. So algorithm picks min.
Thus seems correct.
But wait, one nuance: In the calculation of back_excl, we only count cards with a!=b. However, what about cards where a == b? Those are already counted in front_count for that color, but they also have b = a. For additional count for that color, we must not include them because flipping them doesn't increase count. So back_excl should not count them. Our condition a != b ensures we don't count them.
So algorithm stands.
Now complexity: we have up to 2e5 distinct colors, so we can store maps (hash maps) for front_count and back_excl. Then iterate over all colors present in either map. Using unordered_map in C++ or dict in Python. Then compute answer.
Potential pitfalls:
H = (n+1)//2 (integer division).
Ensure to use long long? But n up to 1e5, moves up to n, fits in int.
If answer remains INF (like >n), output -1.
But is there any scenario where we could achieve majority by using flips that cause a card to show a color that is not originally on either side? No, only two colors per card.
Thus solution done.
But before finalizing, let's think about if there is any scenario where flipping a card that shows C (type B) could allow achieving majority for color C with fewer flips? For instance, suppose we have many cards with a=C and b= some other color D, and also many cards with a=D and b=C. Could we possibly flip some of the type B (a=C) to D, then flip some type C (a=D) to C, and end up with more C? That doesn't make sense because flipping a C away reduces C count, and flipping a D to C increases C count. Net change in C count is zero if we flip one of each, but flips count =2. If we just flip the D to C, we get +1 C at cost 1, which is better than also flipping a C away.
But what about constraints like maybe we need to make sure other colors don't become majority? Not relevant.
Thus per-color optimum is simply to keep all current C's and flip as many backC-only as needed.
Double-check with formal reasoning: For a given target color C, the problem is: choose a subset of cards to flip to minimize flips such that #cards showing C >= H. Let x_i be 0 or 1 indicating whether we flip card i. Then card i shows color: if x_i=0, shows a_i; if x_i=1, shows b_i. We want sum_i [color shown = C] >= H. Minimize sum x_i.
For each card, define:
If a_i = C and b_i = C: contributes 1 always, irrespective of x_i. So we can ignore, as they always add 1.
If a_i = C and b_i != C: contributes 1 if x_i=0, else 0.
If a_i != C and b_i = C: contributes 0 if x_i=0, else 1.
If a_i != C and b_i != C: contributes 0 always.
Thus we have two types of cards that can be influenced: type B (a=C, b!=C) and type C (a!=C, b=C). Let’s denote:
For each type B card: flipping it reduces count by 1, cost 1.
For each type C card: flipping it increases count by 1, cost 1.
Also we have type A (both C) giving +1 fixed.
Let B = number of type B cards, C = number of type C cards. The initial count (with no flips) is A + B. To reach H, we need count >= H. If A+B >= H, no flips needed. Otherwise we need to increase count by at least need = H - (A+B). We can increase count by flipping type C cards (each gives +1) but flipping a type B card would decrease count, which is counterproductive. So we should flip exactly need type C cards, assuming need <= C. That gives total flips = need. Could there be a scenario where flipping some type B cards and more type C cards yields same net count with same or fewer flips? Suppose we flip k type B (reduce count by k) and flip need + k type C (increase count by need + k). Net change = (need + k) - k = need, same net count. Total flips = need + 2k, which is greater than need. So not better.
What about flipping type B and not flipping some type C? That would give less count or require more flips. So indeed optimal is to flip only type C cards as needed.
Thus formula correct.
So we just need to compute for each color C: A+B = number of cards with a_i = C (since those include both type A and type B). And C = number of cards with b_i = C and a_i != C.
Thus answer = min_{C} ( max(0, H - count_a[C]) ) subject to count_a[C] + count_back_excl[C] >= H.
Implementation details:
Use map/dict from color to integer.
Iterate over n lines, for each (a,b):
increase cnt_a[a] by 1
if a != b: increase cnt_b_excl[b] by 1
Compute H = (n+1)//2
ans = INF (e.g., n+1)
For each color in set of keys from cnt_a and cnt_b_excl:
base = cnt_a.get(color,0)
extra = cnt_b_excl.get(color,0)
total = base + extra
if total >= H:
need = max(0, H - base)
ans = min(ans, need)
If ans is INF, print -1 else print ans.
We must be careful about using Python's dictionaries and ensuring we iterate over union of keys. We can collect keys into a set.
Complexity: O(n + number_of_distinct_colors) which is O(n).
Now, let's test with some random cases mentally.
Consider n=5, all cards have distinct front colors, but many share a common back color. For example: (1,10), (2,10), (3,10), (4,10), (5,10). H=3. cnt_a: each distinct, so base for color 10 = 0. cnt_b_excl[10] = 5 (since all a!=b). total=5>=3, need=3. So answer 3. Is that achievable? Initially colors:1,2,3,4,5. Flip any 3 cards to show 10, get at least three 10's, majority. So yes. Could we do with 2 flips? If flip 2 cards, we have two 10's and three distinct other colors, no majority. So need 3. Good.
Consider n=5, cards: (1,2), (1,2), (1,2), (1,2), (5,5). H=3. cnt_a: 1:4, 5:1. cnt_b_excl: for first four, a!=b -> b=2, so cnt_b_excl[2]=4. For fifth, a=b, skip. Now color 1: base=4 total=4 (extra 0) need=0 -> answer 0 because already 4 of color 1 >=3. So answer 0.
Consider n=5, cards: (1,2), (1,2), (3,2), (4,2), (5,2). H=3. cnt_a: 1:2,3:1,4:1,5:1; cnt_b_excl[2] = 5? Let's check: first two: a=1 !=2 -> count 2, third: a=3 !=2 -> count1, fourth: a=4 !=2 -> count1, fifth: a=5 !=2 -> count1 => total 5. So color 2: base=0, extra=5, total=5, need=3. So ans candidate 3. Color 1: base=2, extra=0? (b=2 not 1) total=2<3, impossible. So answer 3. Is it minimal? Could we achieve majority of color 1? Need 3, but only 2 base, and no extra (since no back=1), so impossible. Color 2: need 3 flips. Could we achieve majority of color 2 with 2 flips? Suppose flip two cards: we get two 2's, remaining three show 1,1,3? Actually careful: initially colors: 1,1,3,4,5. After flipping two (say the two 1's to 2): colors:2,2,3,4,5 -> only two 2's, not majority. Flip three: 2,2,2,4,5 -> three 2's, majority. So need 3. So answer 3.
Now, consider a scenario where a color appears as both front and back on many cards, but we might need to flip some front cards away to achieve majority of that same color? No.
All good.
But wait: There is an important nuance: The condition is "at least half of the cards have the same color". It doesn't require that the majority color is unique, just that there exists a color that appears at least ceil(n/2) times. If we have two colors each appearing exactly half (when n even), that's okay? For n even, half = n/2. Condition is at least half, so if we have two colors each n/2, then each appears at least half. So condition satisfied. So for even n, H = n/2? Actually ceil(n/2) for even n is n/2. So both satisfy. Good.
Thus our H = ceil(n/2).
Now, let's ensure the formula works for even n: Example n=4, H=2. Suppose cards: (1,2), (1,2), (3,4), (3,4). Initially colors: 1,1,3,3. Both 1 and 3 appear twice, which is >=2, so answer 0. Our algorithm: cnt_a: 1:2,3:2. For color 1: base=2, total=2>=2, need=0. So ans 0. Good.
Now, what about a scenario where base is less than H, but extra is enough, but we might also have some cards where a==C and b!=C, i.e., type B. In our calculation, we are not counting them as extra but they are already in base. So need = H - base. But suppose base > H? Then need=0. So that's fine.
Now, we must ensure that back_excl counts only cards where a != b and b = C. That's correct.
But what about cards where a = C and b = C? They are counted in base, and they also have b = C, but we must not count them in extra. Our back_excl excludes them because a==b, so they are not added. Good.
Now, is it possible that we need to consider colors that don't appear as a_i or b_i? No.
Now, one more nuance: Could flipping a card that originally shows C (type B) be beneficial if we are trying to achieve majority for some other color D, but then we might also need to flip some cards to C? That's separate.
Thus the algorithm is correct.
Now, we can produce solution code in Python (fast enough for n up to 1e5). Use dictionaries (defaultdict(int)). Since colors can be up to 1e9, dict works.
Potential optimization: Use sys.stdin.read() for faster input.
Now, think about edge case where H = 0? n>=1, so H>=1. So fine.
Now, implement:
def solve():
import sys
data = sys.stdin.read().split()
it = iter(data)
n = int(next(it))
cnt_a = {}
cnt_b_excl = {}
for _ in range(n):
a = int(next(it))
b = int(next(it))
cnt_a[a] = cnt_a.get(a, 0) + 1
if a != b:
cnt_b_excl[b] = cnt_b_excl.get(b, 0) + 1
H = (n + 1) // 2
ans = n + 1 # bigger than max possible flips
# keys union
keys = set(cnt_a.keys()) | set(cnt_b_excl.keys())
for col in keys:
base = cnt_a.get(col, 0)
extra = cnt_b_excl.get(col, 0)
if base + extra >= H:
need = max(0, H - base)
if need < ans:
ans = need
if ans == n + 1:
print(-1)
else:
print(ans)
Edge test: n=3, all distinct both sides same: (1,1),(2,2),(3,3). cnt_a:1,2,3 each 1; cnt_b_excl empty. H=2. For each col, base=1, extra=0, total=1<2. So ans remains >n -> -1. Good.
Now, could there be a scenario where we need to flip a card with a==b? But flipping it doesn't change color; so it would be wasteful. Not needed.
Now, is there any scenario where flipping a card with a==b could be useful? Suppose the card's both sides are the same color X. If we are targeting color X, it's already counted; if we are targeting another color Y, flipping it won't change; so no.
Thus solution is done.
But before finalizing, let's consider if there is any oversight about "at least half". For n even, H = n/2. If we have exactly n/2 of a color, that's acceptable. So condition base+extra >= n/2.
Now, what about the possibility of using multiple colors? Our per-color approach is fine.
Now, double-check that we haven't missed any scenario where we need to flip more than needed because flipping a type C card might also affect other colors, but that doesn't matter.
Alright.
Now, we should confirm with some random brute force for small n to ensure correctness. But given the reasoning, it's sound.
One more subtle case: Suppose there is a card with a=C and b=D, and also a card with a=D and b=C. Could it be optimal for achieving majority of color C to flip both? That would be flipping the first one (C->D) and flipping the second (D->C). The net count of C: first reduces by 1, second increases by 1, net zero. Flips = 2. Not beneficial.
But what about achieving majority of color E? Unrelated.
Thus fine.
Now, time complexity O(n). Memory O(n).
Now, one might think about using default dict for convenience.
We'll produce final answer.
But wait, are there any scenarios where the minimal number of flips is not given by need = H - base? Consider scenario where base already >= H, answer 0. Good. When base < H, need = H - base, provided enough extra. That's the number of type C cards to flip. But is it always possible to achieve exactly H with exactly need flips? Yes, because we flip need cards from the type C set. However, what if some of those type C cards are also needed for something else? No. But we need to ensure that we can flip them without affecting other cards? Flipping is independent. So yes.
One more nuance: The type C cards we flip must be distinct. But we have enough distinct cards (since extra count is the number of such cards). So we pick any need of them.
Thus correct.
Now, there is a potential pitfall: Our back_excl counting includes cards where a!=b. But we need to ensure that when we compute extra for a color C, we don't accidentally include cards where a = C (impossible because if a = C and b = C then a=b, excluded; if a=C and b!=C then a!=b, but then b=C? Actually if a=C and b!=C, then b != C, so it wouldn't be counted in back_excl[C] because we only increment back_excl for b, which is not C. So it's fine. So back_excl[C] only gets increments from cards where b = C and a != C.
But what about cards where a != C, b = C, but also a == something else. That's exactly the type C.
Thus all good.
Now, I'll consider some more complex cases.
Case: n=5, cards: (1,2), (1,2), (2,1), (2,1), (3,3). H=3. Let's compute:
cnt_a: 1:2 (first two), 2:2 (third, fourth), 3:1 (fifth). cnt_b_excl: first two: a!=b so b=2 -> +2; third, fourth: a!=b so b=1 -> +2; fifth: a=b skip. So cnt_b_excl: 2:2, 1:2.
Now check colors:
Color 1: base=2, extra=2 (from b=1) total=4>=3, need = max(0,3-2)=1 -> candidate 1.
Color 2: base=2, extra=2 -> need=1 -> candidate 1.
Color 3: base=1, extra=0 total=1<3.
Thus ans = 1. Is it possible? Initially colors: 1,1,2,2,3. Need 3 of same. Flip one card: If we flip a card that currently shows 2 and back is 1 (type C for color 1), say third card (2->1): then colors become 1,1,1,2,3 -> three 1's -> majority. So 1 flip works. Could we get 0? No, because max count is 2 initially. So ans 1. Good.
Case: n=5, cards: (1,2), (1,2), (1,3), (2,3), (3,1). Let's compute manually to see if our method holds. I'll enumerate:
Card1: (1,2)
Card2: (1,2)
Card3: (1,3)
Card4: (2,3)
Card5: (3,1)
H=3.
cnt_a: 1:3 (cards1,2,3), 2:1 (card4), 3:1 (card5) => base for 1=3, 2=1,3=1.
cnt_b_excl: for each card where a!=b:
Card1: a!=b -> b=2 -> +1 for 2.
Card2: +1 for 2.
Card3: a=1,b=3 -> +1 for 3.
Card4: a=2,b=3 -> +1 for 3.
Card5: a=3,b=1 -> +1 for 1.
So cnt_b_excl: 2:2, 3:2, 1:1.
Now check:
Color1: base=3, extra=1 total=4>=3, need=0 (since base>=3) -> candidate 0. So ans 0. Indeed initially three cards show 1 (cards1,2,3). So majority already. So answer 0. Good.
Now consider scenario where base < H, but extra includes cards that also have a=some other? That's fine.
Now, is there any scenario where we might need to flip more than H - base because flipping some type C cards might cause other colors to also become majority? Not relevant.
Thus solution appears correct.
But before finalizing, I should also consider a scenario where we might achieve majority for a color that is not in the candidate set? No.
Now, we should also think about the possibility of using the same card to achieve two colors? No.
Thus answer stands.
Now, coding in Python: use sys.stdin.buffer for speed.
Now, final answer.
But wait, one more nuance: The problem statement says "at least half of the cards have the same color". It could be interpreted that we need that there exists a color such that the number of cards showing that color is at least n/2. For n odd, n/2 is not integer, so we need > n/2, i.e., ceil(n/2). So we used that. Good.
Now, I'd like to double-check sample 2: n=5, H=3, answer 2. Our algorithm gave 2. Good.
Now, we might also consider scenario where flipping a card that is both sides same (a=b) could be considered a move but does nothing. So we never need to do that. So fine.
Thus we can output answer.
Now, I'll produce final code.
But there is another potential edge: What if there is a color C that appears on many cards, but some of those cards are both sides C, some are only front C. In our calculation base includes both. Good. But we also need to ensure that the extra count for color C does not include cards where a=C and b=C? Our back_excl excludes a=b, so no. Good.
Now, think about scenario where a card has a=C, b=C (both same). It contributes to base. Could it also be considered as extra? No. And flipping it would not change count. So correct.
Now, to be thorough, we should check if the condition is "at least half" meaning n/2 floor? For odd n, "at least half" could be interpreted as at least floor(n/2)+1? Actually half of an odd number is not an integer; "at least half" might be interpreted as at least half of the cards, i.e., ≥ n/2. Since n/2 is not integer, the condition is equivalent to ≥ ceil(n/2) because integer must be ≥ n/2, which implies ≥ ceil(n/2). So it's consistent.
Now, consider n=1, H=1, okay.
Now, the problem might have tricky test where answer is -1: for example, n=3, cards (1,2),(3,4),(5,6). cnt_a: 1,3,5 each 1. cnt_b_excl: 2,4,6 each 1. For any color, base+extra = 1+? Actually for color 1: base=1, extra=0 (b=2,4,6 none=1) total=1<2. Color 2: base=0, extra=1 total=1<2. So all <2 -> -1. Indeed we cannot get two cards of same color because each card has distinct colors and no common color among them? Actually check: card1: sides 1,2; card2: sides 3,4; card3: sides 5,6. No color appears on two cards. So impossible. So answer -1. Good.
Now, what about n=3, cards (1,2),(1,3),(2,3). cnt_a: 1:2 (first two), 2:1 (third). cnt_b_excl: card1: b=2-> +1 for 2, card2: b=3-> +1 for 3, card3: b=3-> +1 for 3. So color 1: base=2, extra=0? (b=1? none) total=2>=2 need=0 -> answer 0. Indeed initially two cards show 1, so majority.
Thus correct.
Now, it's solved.
But I want to double-check the possibility that flipping some cards might create a majority for a color that wasn't initially considered because it appears only on backs of some cards and maybe also on front of some others after flipping? That's already covered: we consider all colors that appear on any side. For a color D, we compute base (front count) and extra (back only). That includes the possibility that after flipping some backs, we get D. So fine.
Now, we can implement.
But wait, there is a subtlety: The extra count we use is the number of cards that have b=C and a!=C. However, what about cards where a=C and b=C? They are already counted in base, and flipping them doesn't change. But suppose we want to achieve majority for color C, and we have many of these both-sides C cards. They are already in base. So no issue.
Now, let's think about a case where we might need to flip a card that has a=C and b=C to get something else? Not needed.
Thus solution stands.
Now, I'll produce final answer.
But before concluding, I want to check the problem statement again: "Help the Little Elephant to find the minimum number of moves needed to make the set of n cards funny." And "If it is impossible to make the set funny, print -1." So yes.
Now, we need to think about the possibility of flipping the same card multiple times? The problem says "In one move the Little Elephant can turn any card to the other side." So flipping a card twice would bring it back to original, costing 2 moves, which is never optimal. So we can consider each card flipped at most once.
Thus our binary decision per card is valid.
Now, we should also ensure that for each color C, the condition base+extra >= H includes the scenario where some cards are both sides C? Already counted.
Now, one more potential nuance: Could it be that we need to consider the possibility of achieving a majority color that is not among the distinct colors, but by flipping, we might create a new color? No.
Thus, final solution.
I'll now produce final answer with explanation.
But wait, I recall similar problems (Codeforces problem "Little Elephant and Cards" maybe?). Actually I think this is Codeforces Round problem "Little Elephant and Cards" (CF 205B? Not sure). But the known solution is exactly that: For each color, compute how many cards initially show it and how many can be turned to show it, then compute needed flips as max(0, (n+1)/2 - initial) if possible. So matches.
Now, but there is a nuance: Some solutions consider also the possibility of flipping cards that initially show the target color to something else? That's not needed.
Thus final.
Now, code.
But let's double-check H calculation: n = 1 => H = (1+1)//2 = 1, correct. n = 2 => (2+1)//2 = 1? Wait integer division: (2+1)//2 = 3//2 = 1. But is half of 2 = 1? Yes, need at least 1? Actually for n=2, "at least half" means at least 1. So 1 is correct. But with n=2, if we have two cards each showing different colors, that's okay because each color appears 1, which is at least 1? Actually the condition is that there exists a color appearing on at least half of the cards. If half is 1, then each color appears 1, so condition satisfied. So yes. So H = 1 for n=2. That's consistent.
But some might argue that for n=2, majority normally requires > half (i.e., 2) but the problem says at least half, so 1 is enough. So it's fine.
Now, test n=2, cards (1,2), (3,4). front colors:1,3. Both appear once, >=1, so condition satisfied. So answer 0. Our algorithm: H=1, base for 1 =1 total=1>=1 need=0 -> ans 0. Good.
Now, test n=2, cards (1,2), (1,3). front: 1,1 => two 1's, H=1, need=0. Good.
Now, test n=2, cards (1,2), (2,1). front:1,2 -> both 1. So condition satisfied (both colors appear once) -> answer 0. Our algorithm: base for 1=1 total=1? Also extra for 1? back_excl: first card b=2, second card b=1? Wait second card a=2,b=1 -> a!=b so back_excl[1]+=1. So for color1: base=1, extra=1 => total=2>=1, need=0. So ans 0.
Thus correct.
Now, test n=4, H=2. cards (1,2),(3,4),(5,6),(7,8). Each front unique, base each =1, extra maybe for colors 2,4,6,8 each 1. For color1: base=1 extra=0 total=1<2 impossible; color2: base=0 extra=1 total=1<2; etc. So no color has total >=2? Wait color2: total=1, color4:1, etc. So answer -1. But is it possible to get at least 2 cards of same color? Let's examine: All colors distinct on each side? Actually sides: (1,2),(3,4),(5,6),(7,8). There is no common color across any two cards. So you cannot have two cards showing the same color. So indeed impossible. So answer -1. Good.
Now, test n=4, cards (1,2),(1,3),(2,3),(4,4). H=2. cnt_a: 1:2 (first two), 2:1 (third), 4:1 (fourth). cnt_b_excl: first: b=2, second: b=3, third: b=3. So back_excl:2:1,3:2. Check colors:
Color1: base=2 total=2>=2 need=0 -> ans0. Indeed two 1's already. So answer 0.
Now, test n=4, cards (1,2),(1,2),(3,4),(4,3). H=2. cnt_a:1:2,3:1,4:1. cnt_b_excl: first two: b=2 each => back_excl[2]=2; third: b=4 => back_excl[4]+=1; fourth: b=3 => back_excl[3]+=1. Check colors:
1: base=2 total=2 need=0 -> ans0. Indeed two 1's. So answer 0.
Now, test n=4, cards (1,2),(2,1),(3,4),(4,3). H=2. cnt_a:1:1,2:1,3:1,4:1. cnt_b_excl: first: b=2, second:b=1, third:b=4, fourth:b=3 => back_excl:2:1,1:1,4:1,3:1. So for color1: base=1 extra=1 total=2 need= max(0,2-1)=1. Similarly others also 1. So ans=1. Is it achievable? Initially colors:1,2,3,4 all distinct. Flip one card: say flip first card to 2 -> colors:2,2,3,4 -> two 2's, majority. So 1 flip works. Could we get 0? No. So ans 1. Good.
Thus algorithm seems correct.
Now, before finalizing, we must consider potential integer overflow or performance issues. None.
Now, what about the possibility of having multiple colors with same minimal need, we pick min.
Now, think about edge case where n is large (1e5) and colors are many, but dictionaries fine.
Now, implement.
Let's code.
But there's one more nuance: In the calculation of back_excl, we increment for b when a != b. However, what if a and b are same? We skip. That's fine.
But what about the case where a != b, but b is the same as a for another card? That's fine.
Now, we must be careful: The condition to be funny is "at least half of the cards have the same color". So if we have multiple colors each with exactly H (when n even and H = n/2), it's fine. So we don't need to ensure that the chosen color is the unique majority.
Now, all good.
Now, consider scenario where we could achieve majority for some color by flipping some cards, but flipping those cards might also cause another color to become majority with fewer flips? That's irrelevant because we are taking min over all colors. So if flipping some cards to achieve color C yields flips f, that's considered. But there might be a scenario where we flip a set of cards that results in a majority for some color that we didn't specifically target, but the flips count is less than the direct per-color minimal? Could that happen? For example, maybe by flipping some cards, we could create a majority for a color that we didn't anticipate because it appears as a side on a card we flipped? But we consider all colors individually. However, the direct per-color minimal we computed assumes we only flip cards that directly increase count of that color. But could there be a better solution where we flip a card that doesn't directly contribute to the final majority color? For instance, suppose we want to achieve a majority of color X. Flipping a card that currently shows X away (cost 1) and flipping another card to X (cost 1) yields net zero change in X count, but might reduce count of some other color Y to below H? Not needed. Or might allow a different color Z to become majority with fewer flips? But we are free to choose any color, so if flipping some cards yields a majority for Z with fewer flips than the direct per-color minimal for Z, then that would be captured by the per-color minimal for Z because we could consider flipping cards to achieve Z directly. But if the set of flips that yields majority for Z includes flipping a card that initially shows Z away, that would be suboptimal relative to just flipping cards that increase Z. So the minimal flips for Z is at most the flips in any solution that results in Z majority. So the per-color direct method gives a lower bound? Actually we need to ensure that the per-color direct method yields the minimal flips among all solutions that result in that color being majority. Suppose we have a solution that results in color Z majority, with flips F. Consider the set of cards that show Z at the end. Some of those may have been flipped, some not. The cards that show Z at the end must be either: initially a_i = Z and not flipped, or b_i = Z and flipped. So the number of cards showing Z is base_kept + flipped_to_Z, where base_kept is subset of type B (a=Z,b!=Z) that we didn't flip, plus type A (a=Z,b=Z) always. The number of flips used includes flipping those that we flipped to Z, and possibly flipping some other cards (including type B away). But if we flipped any type B card (a=Z) away, that would reduce the count of Z, so to still achieve majority, we must compensate by flipping more type C to Z. That would only increase total flips compared to just not flipping that type B. So any optimal solution for color Z would not flip any type B away. Also, flipping any type A (both sides Z) does nothing and adds flips, so not in optimal. Also, flipping any card that is not relevant to Z (type D) doesn't affect Z count but adds flips, so not in optimal. Therefore, for a given target color Z, the optimal solution is to keep all type B as they are, and flip exactly need type C cards, where need = H - (A+B). This yields minimal flips for that color. So per-color direct method gives the exact minimal flips for achieving that color majority. Therefore the overall minimal flips is min over colors of that. So our method is correct.
Thus we can confidently implement.
Now, we need to output answer.
Now, I'll write final solution.
Potential improvement: We could avoid storing union set by iterating over keys of cnt_a and also checking cnt_b_excl keys not in cnt_a, but set union is fine.
Now, code.
But be careful: In Python, using dictionary and then union of keys might be O(k) where k is number of distinct colors, up to 2e5. That's fine.
Now, produce final answer.
After writing code, test with examples:
Example 1:
3
4 7
4 7
7 4
-> ans 0.
Example 2:
5
4 7
7 4
2 11
9 7
1 1
-> ans 2.
Works.
Now, finalize.
But there is one more nuance: The problem might be from Codeforces, and they often require fast I/O. We'll use sys.stdin.buffer.
Now, final code.
Let's include in answer.
--- Additional check: When computing H, we need ceil(n/2). For n=5, H=3; n=6, H=3? Wait (6+1)//2 = 7//2 = 3. But half of 6 is 3, so yes >=3. So correct.
For n=6, if H=3, then condition is that some color appears at least 3 times. That's correct.
Now, check n=6, cards all distinct colors (each front unique, backs unique and different from all others). total distinct colors many, but can we get 3 of same? No. So ans -1.
Now, test n=6 with a scenario: (1,2),(1,2),(1,2),(4,5),(4,5),(4,5). H=3. base for 1:3, total=3, need=0 -> ans 0. So fine.
Now, test n=6, (1,2) repeated 6 times? Actually 6 cards each (1,2). Then base for 1:6? No, front=1 each, base=6, need=0. So ans0.
Now, test n=6, (1,2) * 3 and (3,1) * 3. Compute: cnt_a: 1:3, 3:3. cnt_b_excl: first three: b=2 -> back_excl[2]=3; last three: a=3,b=1 (a!=b) -> back_excl[1]=3. So color 1: base=3, extra=3? Actually extra = back_excl[1] = 3. So total=6, need= max(0,3-3)=0. So ans0 because already 3 of color1. Yes, front of first three are 1, last three are 3; but we have 3 ones initially, which is exactly H=3, so majority exists. Good.
Now, test n=6, where base for all colors less than 3, but combination of flips might produce a color with 3. Our method will find min need.
Now, we also consider if there might be a scenario where we need to flip a card that is type B (a=C) to achieve majority of a different color D with fewer flips than any direct color? But then that solution's final majority color is D. So its flip count will be considered when evaluating D. However, could the solution that flips a type B card away from C and does something else yield a lower flip count for achieving some color than the direct per-color minimum for that color? Suppose we have color D that appears on backs of many cards, but also appears on front of some cards that we would need to flip away? For example, we want majority of D. There are some cards with a=D, b=other (type B for D). According to our direct method, we would keep them as D (cost 0). But what if flipping those cards away (cost 1) allows us to not flip some other cards? That seems nonsensical. Flipping a D away reduces D count, so to compensate we need to flip more other cards to D. So net flips increase. So not better.
But what if flipping a D away also reduces the count of some other color that might become a competing majority? Not relevant; we only need existence of at least one majority. So even if there are two colors each with H, it's still funny. So we don't need to suppress other colors.
Thus per-color optimal is correct.
Now, we can finalize.
One more thought: Could there be a solution that achieves majority by mixing colors? No.
Thus final.
Now, we produce solution.
But wait: The problem statement says "at least half of the cards have the same color". Could it be interpreted as "there exists a color such that the number of cards showing that color is at least n/2 (floor for odd?)" But usually it's ceil. However, sometimes in Codeforces, they define "half" as "strictly more than half"? Let's read sample: In first sample, n=3, they say "Since two of the three cards are of the same color 4, you do not need to change anything". Two is > 1.5, so it's at least 2. That's ceil(3/2)=2. So they used "at least half" meaning ceil. Good.
Second sample: n=5, they need 3 of color 7 after flips. 3 is ceil(5/2)=3. So consistent.
Thus H = (n+1)//2.
Now, implement.
Potential bug: In Python, integer division // gives floor. So (n+1)//2 works for both even and odd.
Now, code.
I'll add a note: If ans remains greater than n (like n+1), output -1.
Now, final answer.
Now, I will write the final solution with explanation.
But we must also consider the possibility that the minimal flips could be greater than n? No, max needed is n (flip all). So INF = n+1 works.
Now, produce final output.
Alright.
Now, I'll write final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.buffer.read().split()
it = iter(data)
n = int(next(it))
cnt_front = {}
cnt_back_excl = {} # cards with b = colour and a != b
for _ in range(n):
a = int(next(it))
b = int(next(it))
cnt_front[a] = cnt_front.get(a, 0) + 1
if a != b:
cnt_back_excl[b] = cnt_back_excl.get(b, 0) + 1
code
H = (n + 1) // 2 # minimum number of cards needed for "funny"
INF = n + 1
ans = INF
# consider every colour that appears on any side
colours = set(cnt_front.keys()) | set(cnt_back_excl.keys())
for col in colours:
base = cnt_front.get(col, 0) # cards already showing col
extra = cnt_back_excl.get(col, 0) # cards that can be flipped to show col
if base + extra >= H:
need = max(0, H - base)
if need < ans:
ans = need
if ans == INF:
print(-1)
else:
print(ans)