#P12532. [XJTUPC 2025] Primal Core Optimization: Attribute Balance

[XJTUPC 2025] Primal Core Optimization: Attribute Balance

Description

You are playing a game called "Rhythm Sin". In this game, you control partners to explore the "World".

Each partner has three attributes: Stop\mathrm{Stop} (S), Flag\mathrm{Flag} (F), and Ever\mathrm{Ever} (E). When all partners' attributes are completely consistent, they can successfully fuse.

Your goal is to fuse all partners, meaning you must make every pair of partners have identical attributes. To achieve this, you need to modify their attributes. Each modification consists of the following steps:

  • Select any partner as the target (you can only select one partner).
  • Choose any subset of her attributes.
  • Increment or decrement all selected attributes by 11 (the same operation must be carried out simultaneously, i.e., all selected attributes increment or all selected attributes decrement).

Due to limited magical power, each modification costs 11 primal stone. Your task is to determine the minimum number of primal stones required to achieve your goal.

Input Format

The first line contains an integer NN (1N1051\le N\le 10^5), the number of partners.

The next NN lines each contain three integers SiS_i, FiF_i, and EiE_i (1Si,Fi,Ei1051\le S_i, F_i, E_i \le 10^5), separated by a space, representing the values of the three attributes for the ii-th partner.

Output Format

A single integer representing the minimum number of primal stones required.

3
2 2 2
2 2 2
1 2 3
2
3
2 3 1
3 5 3
3 2 1
4
1
201 502 10
0
4
1 2 3
1 2 3
4 5 6
4 5 6

6

Hint

For the first sample, one possible sequence of modifications (costing 22 primal stones) is:

  • Select partner 33 and S attribute, modify her attributes to 22, 22, 33;
  • Select partner 33 and E attribute, modify her attributes to 22, 22, 22.

It can be proved that you cannot achieve the goal using less than 22 primal stones, so the answer is 22.

For the second sample, one possible sequence of modifications (costing 44 primal stones) is:

  • Select partner 11 and S and E attributes, modify her attributes to 33, 33, 22;
  • Select partner 22 and F and E attributes, modify her attributes to 33, 44, 22;
  • Select partner 22 again and F attribute, modify her attributes to 33, 33, 22;
  • Select partner 33 and F and E attributes, modify her attributes to 33, 33, 22.

It can be proved that you cannot achieve the goal using less than 44 primal stones, so the answer is 44.

For the third sample, because there is only one partner, you don't need to perform any modifications, so the answer is 00.