#P11004. Mystical Rune Journey

    ID: 13054 Type: Default 1000ms 256MiB

Mystical Rune Journey

Mystical Rune Journey

In the Blue Bridge Kingdom, two magicians, Little Blue and Little Bridge, are entrusted with maintaining the order of time and space. Each holds N runestones. Little Blue’s stones are labeled \(s_1, s_2, \ldots, s_N\), and Little Bridge’s are labeled \(t_1, t_2, \ldots, t_N\). Every runestone carries a powerful number between 1 and \(10^9\).

The magicians embark on a journey using these runestones under a strict rule. First, Little Blue chooses a runestone \(s_i\) to travel to a new node. Then, Little Bridge must select a runestone \(t_j\) with an index \(j > i\) to continue the journey. Likewise, after Little Bridge’s move, Little Blue selects another runestone \(s_k\) with \(k > j\) and so on. Hence, a valid journey sequence has the form:

[ s_{i_1},\ t_{i_2},\ s_{i_3},\ t_{i_4},\ \ldots \quad \text{with} \quad i_1 < i_2 < i_3 < i_4 < \cdots ]

There is an additional twist: consecutive runestones must resonate with each other. The resonance occurs if ***at least one*** of the two numbers contains one of the specific digits: \(0\) (spark), \(2\) (water ripple), or \(4\) (wind whisper). For example, the sequence 126, 552, 24, 4 is valid because every adjacent pair meets the resonance criterion, while the sequence 15, 51, 5 is not valid.

Little Blue always initiates the journey. Your task is to compute the length of the longest possible journey sequence the magicians can perform following these rules.

inputFormat

The input contains three lines:

  1. An integer \(N\) representing the number of runestones each magician holds.
  2. N space-separated integers representing Little Blue's runestones: \(s_1, s_2, \dots, s_N\).
  3. N space-separated integers representing Little Bridge's runestones: \(t_1, t_2, \dots, t_N\).

outputFormat

Output a single integer: the length of the longest valid journey sequence.

sample

3
124 135 360
45 26 89
3