#include <bits/stdc++.h>
using namespace std;
int used[105], c[105];
int main() {
  string a, b;
  int rep1, rep2;
  cin >> rep1 >> rep2;
  cin >> a >> b;
  int len1 = a.size(), len2 = b.size();
  int limite = min(rep1 * len1, len1 * len2 * (len1 + 1));
  int posb = 0, cant = 0;
  memset(used, -1, sizeof used);
  used[0] = c[0] = 0;
  int atam = -1, parcial = -1, p = 0;
  for (int i = 0; i < limite; ++i)
    if (a[i % len1] == b[posb % len2]) {
      posb++;
      if (posb % len2 == 0) {
        cant++;
        if (used[(i + 1) % len1] != -1) {
          atam = i + 1 - used[(i + 1) % len1];
          parcial = used[(i + 1) % len1];
          cant -= c[(i + 1) % len1];
          p = c[(i + 1) % len1];
          break;
        }
        used[(i + 1) % len1] = i + 1;
        c[(i + 1) % len1] = cant;
      }
    }
  int answer = (cant + p) / rep2;
  if (atam != -1) {
    int t = (rep1 * len1 - parcial) / atam;
    cant *= t;
    posb = 0;
    for (int i = atam * t + parcial; i < len1 * rep1; ++i)
      if (a[i % len1] == b[posb % len2]) {
        posb++;
        if (posb % len2 == 0) cant++;
      }
    answer = max(answer, (cant + p) / rep2);
  }
  cout << answer << endl;
  return 0;
}
