# ADA University - February 12 - LCA

# LCA Problem Revisited (Easy)

Given the hanging tree with **n** (**1** ≤ **n** ≤ **100**) vertices, numbered from **0** to **n** - **1**. You need to answer **m** (**1** ≤ **m** ≤ **100**) queries about the least common ancestor for the given pair of vertices.

The queries are generated next way. Given the numbers `a`

, _{1}`a`

and numbers _{2}**x**, **y** and **z**. The numbers `a`

, ..., _{3}`a`

are generated next way: _{2m}`a`

= (_{i}**x** · `a`

+ _{i-2}**y** · `a`

+ _{i-1}**z**) mod **n**. The first query has the form <`a`

, _{1}`a`

>. If the answer to the (_{2}**i** - **1**)-th query is **v**, then the **i**-th query has the form <(`a`

+ _{2i-1}**v**) mod **n**, `a`

>._{2i}

#### Input

The first line contains two integers **n** and **m**. The root of the tree has number **0**. The second line contains **n** - **1** integers, the **i**-th of which equals to the parent number of vertex **i**. The third line contains two integers in the range from **0** to **n** - **1**: `a`

and _{1}`a`

. The fourth line contains three integers _{2}**x**, **y** and **z**, these numbers are nonnegative and do not exceed `10`

.^{9}

#### Output

Print the sum of vertex numbers - the answers to the queries.

3 2 0 1 2 1 1 1 0

2