#include <bits/stdc++.h>
using namespace std;
long long MOD = (long long)1000000000 + 7;
const long long INF = 1e17;
const double pi = acos(-1);
const double EPS = 1e-10;
const long long MAX = 200020;
long long dp[2020][2020];
long long N, M;
long long S[2020], T[2020];
long long cntS[10][2020], cntT[10][2020];
long long idxS[10][2020], idxT[10][2020];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  dp[0][0] = 1;
  dp[0][1] = 1;
  dp[1][0] = 1;
  for (long long i = 0; i < 10; i++) {
    for (long long j = 0; j < 2020; j++) idxS[i][j] = 0;
  }
  for (long long i = 0; i < N; i++) {
    cin >> S[i];
    for (long long j = 0; j < 10; j++) {
      cntS[j][i + 1] = cntS[j][i];
      idxS[j][i + 1] = idxS[j][i];
    }
    idxS[S[i]][i + 1] = i + 1;
    cntS[S[i]][i + 1] += 1;
  }
  for (long long i = 0; i < M; i++) {
    cin >> T[i];
    for (long long j = 0; j < 10; j++) {
      cntT[j][i + 1] = cntT[j][i];
      idxT[j][i + 1] = idxT[j][i];
    }
    idxT[T[i]][i + 1] = i + 1;
    cntT[T[i]][i + 1] += 1;
  }
  for (long long i = 0; i < 2020; i++) {
    dp[0][i] = 1;
    dp[i][0] = 1;
  }
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < M; j++) {
      dp[i + 1][j + 1] = dp[i + 1][j];
      if (idxS[T[j]][i + 1] != 0) {
        long long idx1 = idxS[T[j]][i + 1];
        cout << idx1 << endl;
        ;
        if (1) {
          dp[i + 1][j + 1] += dp[idx1][j];
        }
      }
      cout << i + 1 << " ";
      cout << j + 1 << " ";
      cout << dp[i + 1][j + 1] << endl;
      ;
    }
  }
  cout << dp[N][M] << endl;
  ;
}
