#include <bits/stdc++.h>
using namespace std;
struct S {
  long long start, stcount;
};
long long ta[26] = {0}, tb[26] = {0};
S s[110];
long long m1, m2, n1, n2;
string s1, ws1, s2;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> m1 >> m2;
  cin >> s1 >> s2;
  n1 = s1.size();
  n2 = s2.size();
  long long t = 0;
  for (long long i = 0; i < n1; i++) {
    ta[s1[i]]++;
  }
  for (long long i = 0; i < n1; i++) {
    tb[s1[i]]++;
  }
  for (long long i = 0; i < 26; i++) {
    if (tb[i] && ta[i] == 0) {
      cout << 0;
      return 0;
    }
  }
  if (n1 * m1 >= 100000) {
    ws1 = "";
    while (t < 100000) {
      ws1 += s1;
      t += n1;
    }
  } else {
    ws1 = "";
    while (t < m1 * n1) {
      ws1 += s1;
      t += n1;
    }
  }
  memset(s, -1, sizeof(s));
  long long x = 0, tt = 0;
  s[0].start = 0;
  s[0].stcount = 0;
  long long count = 0;
  for (long long i = 0; i < t; i++) {
    if (s2[x] == ws1[i]) {
      x++;
    }
    if (x == n2) {
      count++;
      if (s[(i + 1) % n1].start != -1) {
        long long xlen = i - s[(i + 1) % n1].start + 1;
        long long remlen = n1 * m1 - i - 1;
        count += (remlen / xlen) * (count - s[(i + 1) % n1].stcount);
        long long temp = (remlen) % xlen;
        int x = 0, j = t - temp;
        while (j < t) {
          if (s2[x] == ws1[j]) {
            x++;
          }
          j++;
          if (x == n2) {
            count++;
            x = 0;
          }
        }
        cout << count / m2 << endl;
        return 0;
      } else {
        s[(i + 1) % n1].start = i + 1;
        s[(i + 1) % n1].stcount = count;
        x = 0;
      }
    }
  }
  cout << 0;
  return 0;
}
