Why Did the Cow Cross the Road 11

Given a permutation of breeds on each side of a road, connect pairs whose breed numbers differ by at most 4 using non-crossing edges, maximizing the count.

Hard8Dynamic programmingDivide and conquerSortingSegment treeNo attempts yetTime limit2sMemory limit512 MB

Problem

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 2N2N pastures there was an N×NN \times 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 NN breeds of cows, numbered breed 1, breed 2, ..., breed NN. Cows of breed aa and breed bb are friendly if ab4|a-b| \le 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 NN 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.

Input

The first line contains NN (1N1000001 \le N \le 100\,000). Each of the next NN lines gives the breed number of the cows in the pastures on the left side of the road, in order. Each breed appears exactly once. The following NN lines describe the pastures on the right side of the road in the same way.

Output

Print the maximum number of crosswalks that can be built while satisfying the conditions.