/************************************************
Grundlegende Algorithmen mit Java,
http://algorithmen-und-problemloesungen.de/
Copyright @2007-2009 by Doina Logofatu
************************************************/

import java.io.*;
import java.util.*;

public class P03LCS {
  private static final String FileInputName = "lcs.in";
  private static final String FileOutputName = "lcs.out";

  public static void main(String[] args) throws IOException {
    Scanner scanner = null;
    PrintStream out = null;
    try {
      scanner = new Scanner(new File(FileInputName));
      out = new PrintStream(new File(FileOutputName));

      if (!scanner.hasNextInt())
        return;
      int m = scanner.nextInt();
      if (!scanner.hasNextInt())
        return;
      int n = scanner.nextInt();

      int x[] = new int[m];
      for (int i = 0; i < m; i++) {
        if (!scanner.hasNextInt())
          return;
        x[i] = scanner.nextInt();
      }
      int y[] = new int[n];
      for (int i = 0; i < n; i++) {
        if (!scanner.hasNextInt())
          return;
        y[i] = scanner.nextInt();
      }

      int[][] c = new int[m + 1][n + 1];
      for (int i = 1; i <= m; ++i)
        for (int j = 1; j <= n; ++j) {
          if (x[i - 1] == y[j - 1]) {
            c[i][j] = c[i - 1][j - 1] + 1;
            continue;
          }
          ;
          if (c[i - 1][j] >= c[i][j - 1]) {
            c[i][j] = c[i - 1][j];
          } else {
            c[i][j] = c[i][j - 1];
          }
        }
      int ll = m, cc = n;
      List<Integer> v = new ArrayList<Integer>();
      while (ll != 0 || cc != 0) {
        if (ll != 0 && cc != 0 && x[ll - 1] == y[cc - 1]) {
          v.add(x[ll - 1]);
          --ll;
          --cc;
          continue;
        }
        if (ll != 0 && c[ll][cc] == c[ll - 1][cc]) {
          ll--;
          continue;
        }
        if (cc != 0 && c[ll][cc] == c[ll][cc - 1]) {
          cc--;
        }
      }

      out.printf("Maximale Laenge: %d%n", c[m][n]);
      ListIterator<Integer> lit = v.listIterator(v.size());
      while (lit.hasPrevious()) {
        out.print(lit.previous());
        out.print(' ');
      }
    } finally {
      if (scanner != null) {
        scanner.close();
      }
      if (out != null) {
        out.close();
      }
    }
  }
}

