eolymp
bolt
Try our new interface for solving problems
Problems

Ibrahim's dissertation

Ibrahim's dissertation

Ibrahim is finishing his studies this year and is exploring the longest common subsequence for a small part of his dissertation. In his research, he needed to find the longest common subsequence of permutations. Ibrahim is not good at permutations. Help him with this. You are given the number of permutations $k$. Each permutation is a sequence of numbers $1, 2, ..., n$ in arbitrary order. Find the length of the longest common subsequence of the given permutations. \textbf{Note 1.} A sequence of numbers $1, 2, ..., n$ written in arbitrary order is called a permutation of $n$ elements. \textbf{Note 2.} A subsequence of a sequence is a sequence that can be obtained from the given sequence by removing some elements without changing the order of the remaining elements or without removing any elements. A subsequence that belongs to two or more sequences is called a common subsequence of these sequences. \InputFile The first line contains two integers $n~(1 \le n \le 1000)$ and $k~(2 \le k \le 5)$. Each of the next $k$ lines contains a permutation consisting of $n$ integers $1, 2, ..., n$. \OutputFile Print the length of the longest common subsequence of the given permutations. \Examples In the first example, the sequence $2~3~5$ or $2~4~5$ is the longest common subsequence. They both appear in both permutations. In the second example, $1~2~3$ is the longest common subsequence. It appears in all three permutations.
Time limit 1 second
Memory limit 128 MiB
Input example #1
5 2
1 2 3 4 5
2 4 3 5 1
Output example #1
3
Input example #2
4 3
1 4 2 3
4 1 2 3
1 2 4 3
Output example #2
3
Source 2024, Azerbaijan Republic Informatics Olympiad, Semifinals, February 18