We found a way to solve John's problem, but we got lost on the way to tell him. We did reach John's farm, yet instead of 2N pastures there was an N×N grid. It turned out we had visited the farm of a different man with the same name. Meanwhile, the John we wanted to help kept getting wrong-answer and runtime-error verdicts no matter how much he debugged his code, so he gave up and is now trying a second plan.
John recently learned that some breeds are friendly with each other. His farm has N breeds of cows, numbered breed 1, breed 2, ..., breed N. Cows of breed a and breed b are friendly if ∣a−b∣≤4; otherwise they do not get along.
For the new members of the Help John Association, here is the layout of the farm again. A straight road runs through the farm, with N pastures on each side. On the left side, each breed occupies exactly one pasture, and the same holds on the right side. To prevent traffic accidents, John wants to build crosswalks. Each crosswalk connects one pasture on the left with one pasture on the right, and it does not need to be perpendicular to the road. A crosswalk may only connect two pastures whose cows are friendly. Each pasture can have at most one crosswalk, and no two crosswalks may cross.
Help John build as many crosswalks as possible under these rules.