Why Did the Cow Cross the Road II (Platinum)#10542

Why Did the Cow Cross the Road II (Platinum)

1 seconde
128 mégaoctets
Difficile

Farmer John is continuing to ponder the problem of cows crossing the road through his farm, introduced in the preceding problem. He realizes that interactions between some pairs of breeds are actually acceptable if the breeds are friendly, a property that can be characterized simply in terms of breed IDs: breeds and are friendly if |a - b| \le 4, and unfriendly otherwise. It is acceptable for cows to wander into fields designated for other breeds, as long as those breeds are friendly.

Given the ordering of the fields on both sides of the road through FJ's farm (with exactly one field for each breed on each side), help FJ determine the maximum number of crosswalks he can build across the road such that no two crosswalks intersect and each crosswalk connects two fields designated for friendly breeds. Each field can be connected by at most one crosswalk, so no two crosswalks may share an endpoint.

Entrée

The first line contains number n~(1 \le n \le 10^5). The next lines describe the order of the fields on one side of the road by breed ID. Each breed ID is an integer from to .

The following lines describe the order of the fields on the other side of the road, again by breed ID. Each breed ID appears exactly once in each ordering.

Sortie

Print the maximum number of disjoint "friendly crosswalks" Farmer John can build across the road.

Exemples

Exemple 1

Loading
Loading