In this problem, we will define a graph called star graph, and the question is to find the minimum distance between two given nodes in the star graph.
Given an integer n, an n?dimensional star graph, also referred to as S?n??, is an undirected graph consisting of n! nodes (or vertices) and ((n?1) ? n!)/2 edges. Each node is uniquely assigned a label x?1?? x?2?? ... x?n??which is any permutation of the n digits 1,2,3,...,n. For instance, an S?4?? has the following 24 nodes 1234,1243,1324,1342,1423,1432,2134,2143,2314,2341,2413,2431,3124,3142,3214,3241,3412,3421,4123,4132,4213,4231,4312,4321. For each node with label x?1?? x?2??x?3?? x?4?? ... x?n??, it has n?1 edges connecting to nodes x?2?? x?1?? x?3?? x?4?? ... x?n??, x?3?? x?2?? x?1?? x?4?? ... x?n??, x?4?? x?2?? x?3?? x?1?? ... x?n??, ..., and x?n?? x?2?? x?3?? x?4?? ... x?1??. That is, the n?1 adjacent nodes are obtained by swapping the first symbol and the d?th symbol of x?1?? x?2?? x?3?? x?4?? ... x?n??, for d=2,...,n. For instance, in S?4??, node 1234 has 3 edges connecting to nodes 2134, 3214, and 4231. The following figure shows how S?4?? looks (note that the symbols a, b, c, and d are not nodes; we only use them to show the connectivity between nodes; this is for the clarity of the figure).
In this problem, you are given the following inputs:
- n: the dimension of the star graph. We assume that n ranges from 4 to 9.
- Two nodes x?1?? x?2?? x?3?? ... x?n?? and y?1?? y?2?? y?3?? ... y?n?? in S?n??.
You have to calculate the distance between these two nodes (which is an integer).
Input Format
n (dimension of the star graph)
A list of 5 pairs of nodes.
Output Format
A list of 5 values, each representing the distance of a pair of nodes.
样例输入
4
1234 4231
1234 3124
2341 1324
3214 4213
3214 2143
样例输出
1
2
2
1
3
题目来源
2017 ACM-ICPC 亚洲区(南宁赛区)网络赛