#P6119. [USACO17FEB] Why Did the Cow Cross the Road II G

[USACO17FEB] Why Did the Cow Cross the Road II G

Description

Farmer John raises NN breeds of cows, numbered from 11 to NN. Some breeds get along well with others. It turns out that for two breeds numbered a,ba, b, if ab4|a-b| \leq 4, then these two breeds can get along; otherwise, they cannot.

A long road runs through the entire farm. There are NN pastures on the left side of the road (each breed of cow occupies exactly one pasture), and there are also NN pastures on the right side of the road (each breed of cow occupies exactly one pasture). To help the cows cross the road safely, Farmer John wants to draw some crosswalks (cow-walks?) on the road, which must satisfy the following conditions:

  1. A crosswalk starts from some pasture on the left side and connects to some pasture on the right side.
  2. The cows in the two connected pastures must be able to get along.
  3. Crosswalks cannot intersect in the middle of the road.
  4. Each pasture can be connected to at most one crosswalk.

You need to help FJ find the maximum number of crosswalks that can be drawn.

Input Format

The first line contains an integer NN (1N10001 \leq N \leq 1000).

The next NN lines each contain an integer aia_i, representing the breed number of the cows in the ii-th pasture on the left side of the road.

The next NN lines each contain an integer bib_i, representing the breed number of the cows in the ii-th pasture on the right side of the road.

Output Format

Output the maximum number of crosswalks that can be drawn.

6
1
2
3
4
5
6
6
5
4
3
2
1
5

Hint

Translated by ChatGPT 5