import java.io.*;
import java.util.*;

public class interview_pk {
	public static void main(String[] args) {
		new interview_pk().run();
	}

	BufferedReader br;
	StringTokenizer in;
	PrintWriter out;

	public String nextToken() throws IOException {
		while (in == null || !in.hasMoreTokens()) {
			in = new StringTokenizer(br.readLine());
		}
		return in.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public void solve() throws IOException {
		int n = nextInt();
		int m = nextInt();
		assert((1 <= n) && (n <= 5000));
		assert((1 <= m) && (m <= 5000));
		int[] a = new int[n];
		int[] b = new int[m];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
			assert((1 <= a[i]) && (a[i] <= 10000));
		}
		for (int i = 0; i < m; i++) {
			b[i] = nextInt();
			assert((1 <= b[i]) && (b[i] <= 10000));
		}
		int[] c = new int[m];
		int ans = 0;
		for (int i = 0; i < n; i++) {
			int cur = 0;
			for (int j = 0; j < m; j++) {
				if ((a[i] == b[j]) && (cur + 1 > c[j])) {
					c[j] = cur + 1;
					if (c[j] > ans)
						ans = c[j];
				}
				if ((a[i] > b[j]) && (cur < c[j])) {
					cur = c[j];
				}
			}
		}
		out.println(ans);
	}

	public void run() {
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			br = new BufferedReader(new FileReader("interview.in"));
			out = new PrintWriter("interview.out");

			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
}