This page is still under construction.

Parts of this page are still being built. What you see may change.

Genomes

Time limit1sMemory limit128 MB

Summary
Find the length of the longest common subsequence of up to 20 permutations of size up to 500.
Level

Medium6 of 10

Topics
Graph, Topological sort, Dynamic programming
Solved
No attempts yet

Problem

Biologists working in comparative genetics are looking for gene sequences that are conserved across a set of species.

Let the set {1,2,…,n}\{1, 2, \dots, n\} denote the genes, so that each number corresponds to one gene. Every species is described by a permutation of 1,2,…,n1, 2, \dots, n that gives the ordering of its genes. A gene sequence x1,x2,…,xkx_1, x_2, \dots, x_k is a conserved gene sequence for a set of species if it is a subsequence (not necessarily contiguous) of the gene ordering of every one of those species.

Write a program that reads the gene orderings from standard input, determines the length of the longest conserved sequence, and writes that length to standard output.

Input

The first line contains two integers nn and mm separated by a single space, with 1≤n≤5001 \le n \le 500 and 1≤m≤201 \le m \le 20. Here nn is the number of genes and mm is the number of species. Each of the next mm lines contains the genome of one species, written as a permutation of 1,2,…,n1, 2, \dots, n with the numbers separated by single spaces.

Output

Print a single integer equal to the length of the longest conserved gene sequence.

Examples4

  1. Example 1

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

    Input
    1 1
    1
    
    Expected output
    1
    
  3. Example 3

    Input
    1 20
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    
    Expected output
    1
    
  4. Example 4

    Input
    5 1
    3 1 2 5 4
    
    Expected output
    5