Under construction

Still being built. Things may change

GAIA Module Installation

Time limit2.5sMemory limit1024 MB

Summary
Given permutations p and q, choose for each slot either p_i or q_{p_i} so that no forbidden pair of modules ends up in adjacent slots, or report -1.
Level

Medium7 of 10

Topics
Graph, DFS
Solved
No attempts yet

Statement

The GAIA system has nn modules, named MINERVA, AETHER, and others. The modules are numbered from 1 to nn, and there are exactly nn slots for connecting them to GAIA. Initially, module ii is connected to slot ii.

There are only two operations that rearrange modules across slots. They are described by permutations pp and qq of length nn. Operation pp moves the module connected to slot pip_i into slot ii. Operation qq works the same way: the module connected to slot qiq_i is reconnected to slot ii.

For GAIA to work correctly, each module must be assigned a slot using a partial composition of pp and qq. That means that for each ii, slot ii must be connected to either

  • the module obtained by applying pp, or
  • the module obtained by applying pp and then qq.

In other words, slot ii can hold module pip_i or module (q∘p)i=qpi(q \circ p)_i = q_{p_i}. The choice can be made independently for each ii.

There are also mm system constraints. Each one says that module aia_i cannot be placed in a slot adjacent to module bib_i.

Input

The first line contains two integers nn and mm (1⩽n⩽2⋅1051 \leqslant n \leqslant 2 \cdot 10^5; 0⩽m⩽2⋅1050 \leqslant m \leqslant 2 \cdot 10^5), the number of modules and the number of constraints.

The second and third lines contain the elements of permutations pp and qq (1⩽pi,qi⩽n1 \leqslant p_i, q_i \leqslant n). Each number from 1 to nn appears exactly once in each line.

Each of the following mm lines contains two integers aia_i and bib_i (1⩽ai,bi⩽n1 \leqslant a_i, b_i \leqslant n; ai≠bia_i \neq b_i), the modules that must not be in adjacent slots.

Output

If no valid assignment exists, print −1-1.

Otherwise, print nn integers separated by spaces. The ii-th integer is 1 if slot ii uses the assignment from pp, and 2 if it uses the assignment from q∘pq \circ p.

If several valid assignments exist, print any of them.

Examples3

  1. Example 1

    Input
    3 1
    3 1 2
    2 1 3
    1 3
    
    Expected output
    1 2 2
    
  2. Example 2

    Input
    3 1
    1 2 3
    3 2 1
    1 2
    
    Expected output
    -1
    
  3. Example 3

    Input
    4 2
    3 4 1 2
    4 1 3 2
    1 2
    3 4
    
    Expected output
    1 2 2 2