1866: Permutations. 置换

内存限制:256 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:5 解决:5

题目描述

We remind that the permutation of some final set is a one-to-one mapping of the set onto itself. Less formally, that is a way to reorder elements of the set. For example, one can define a permutation of the set {1,2,3,4,5} as follows:
组合的置换是它本身的一对一的映射组合。 比较不正式的来说,那是重新排列组合的元素的一种方法。 举例来说,可以定义组合 {1,2,3,4,5} 的一个置换如下:

This record defines a permutation P as follows: P(1) = 4, P(2) = 1, P(3) = 5, etc.
What is the value of the expression P(P(1))? It’s clear, that P(P(1)) = P(4) = 2. And P(P(3)) = P(5) = 3. One can easily see that if P(n) is a permutation then P(P(n)) is a permutation as well. In our example (believe us)
这可依下列各项来定义置换 P: P(1)=4,P(2)=1,P(3)=5,等等。
表达式 P(P(1)) 的值是什么? 很清楚的, P(P(1))=P(4)=2. 而且 P(P(3))=P(5)=3. 可以容易地看出,如果 P(n) 是一个置换,那么 P(P(n))也是一个置换。 在我们的例子中 (请相信我们)

It is natural to denote this permutation by P2(n) = P(P(n)). In a general form the defenition is as follows: P(n) = P1(n), Pk(n) = P(Pk-1(n)). Among the permutations there is a very important one ― that moves nothing:
P2(n) 可表示为 P(P(n)). 按照定义,一般形式如下: P(n) = P1(n), Pk(n) = P(Pk-1(n)). 没有移动在置换之中是非常重要的:

It is clear that for every k the following relation is satisfied: (EN)k = EN. The following less trivial statement is correct (we won’t prove it here, you may prove it yourself incidentally): Let P(n) be some permutation of an N elements set. Then there exists a natural number k, that Pk = EN. The least natural k such that Pk = EN is called an order of the permutation P.
可以很清楚的看出,所有 k 都满足: (EN)k = EN。设 P(n) 是一个 N 元素组合的置换,则存在自然数 k, 使Pk = EN。满足 Pk = EN 的最小自然数 k 就叫做置换 P 的一个顺序。

Problem

The problem that your program should solve is formulated now in a very simple manner: "Given a permutation find its order."
给出一个置换,找出它的顺序。

输入

In the first line of the standard input an only natural number N (1 <= N <= 1000) is contained, that is a number of elements in the set that is rearranged by this permutation. In the second line there are N natural numbers of the range from 1 up to N, separated by a space, that define a permutation ― the numbers P(1), P(2),…, P(N).
第一行包含一个自然数 N (1 <= N <= 1000), N 是置换的元素个数。第二行是 N 个范围为1到N的自然数,用空格隔开,分别表示 P(1), P(2),…, P(N)。

输出

You should write an only natural number to the standard output, that is an order of the permutation. You may consider that an answer shouldn’t exceed 109.
输出置换的顺序,结果不超过109

样例输入 复制

5
4 1 5 2 3

样例输出 复制

6