#include <bits/stdc++.h>
using namespace std;
string A, B;
long long nA, nB;
bool occur[301];
long long needs[101];
long long loopA, loopB;
long long reqLength(long long copies) {
  if (copies <= loopB) return needs[copies];
  long long len = loopB - loopA;
  long long nLoops = (copies - loopB) / len;
  long long ret = 0;
  copies -= nLoops * len;
  ret += nLoops * (needs[loopB] - needs[loopA]);
  while (copies > loopB) {
    copies -= len;
    ret += (needs[loopB] - needs[loopA]);
  }
  ret += needs[copies];
  return ret;
}
int MAIN() {
  while (cin >> nA >> nB >> A >> B) {
    memset(occur, false, sizeof(occur));
    for (int i = 0; i < A.length(); i++) occur[A[i]] = true;
    bool noSol = false;
    for (int i = 0; i < B.length(); i++)
      if (occur[B[i]] == false) noSol = true;
    if (noSol) {
      cout << 0 << endl;
      break;
    }
    needs[0] = 0;
    string superA = "";
    for (int i = 1; i <= 101; i++) superA = superA + A;
    int p = 0;
    for (int i = 1; i <= 101; i++) {
      for (int j = 0; j < B.length(); j++) {
        while (superA[p] != B[j]) p++;
        p++;
      }
      needs[i] = p;
      bool loopOut = false;
      for (int j = 0; j < i; j++)
        if ((needs[i] - needs[j]) % A.length() == 0) {
          loopA = j;
          loopB = i;
          loopOut = true;
        }
      if (loopOut) break;
    }
    long long L = 0, R = 1000000001LL, M;
    while (R - L > 1) {
      M = (L + R) / 2;
      if (reqLength(M * nB) <= nA * A.length())
        L = M;
      else
        R = M;
    }
    cout << L << endl;
  }
  return 0;
}
int main() {
  ios ::sync_with_stdio(false);
  cout << fixed << setprecision(16);
  return MAIN();
}
