#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
const int INF = (int)1e9 + 7, mod = (int)1e9 + 9, pw = 31, N = (int)1e5 + 123,
          M = (int)1e6 + 123;
const double eps = 1e-11;
const long long inf = 1e18;
int n, m;
string second, t;
vector<int> v;
bool used[105];
int last[105];
ll slow() {
  string a = "", b = "";
  for (int i = 1; i <= n; i++) a += second;
  for (int i = 1; i <= m; i++) b += t;
  int i = 0;
  int res = 0;
  for (auto c : a) {
    if (c == b[i]) i++;
    if (i == b.size()) {
      res++;
      i = 0;
    }
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> m;
  cin >> second >> t;
  int i = 0, now = 0;
  ll ans = 0;
  int len = 0;
  for (; v.size() < n && !used[now];) {
    v.push_back(now);
    used[now] = 1;
    for (auto c : second) {
      if (c == t[i]) i++;
      if (i == t.size()) {
        last[now]++;
        i = 0;
      }
    }
    now = i;
  }
  int cnt = 0;
  for (auto i : v) {
    if (len) len++;
    if (i == now) len++;
    if (!len) ans += last[i];
    if (len) cnt += last[i];
  }
  ll x = ans % m;
  ans /= m;
  n -= (v.size() - len);
  ans += (1ll * (n / len) * cnt) / m;
  x += (1ll * (n / len) * cnt) % m;
  int y = n % len;
  len = 0;
  for (auto i : v) {
    if (len) len++;
    if (i == now) len++;
    if (len && len <= y) x += last[i];
  }
  cout << ans + x / m;
  return 0;
}
