#include <bits/stdc++.h>
char c[5000005], x[5000005];
int a[5000005], b[5000005], i, j, k, m, n, o[5000005], p[5000005], q, y;
inline int min(int u, int v) { return u < v ? u : v; }
inline int mod(int z) { return z < n ? z : z - n; }
inline bool check(char* u, char* v, int w) {
  for (; w--; u++)
    if (*u != *--v) return *u < *v;
  return true;
}
inline void copy(char* u, char* v, int w) {
  while (w--) *u++ = *--v;
}
int main() {
  scanf("%s%d", c, &m), n = strlen(c);
  for (i = 0, j = n - 1; i < j; i++, j--) c[i] ^= c[j] ^= c[i] ^= c[j];
  for (i = 0; i < n; o[++q] = i, p[q] = k - j) {
    for (j = i, k = i + 1; k < n && c[j] <= c[k]; k++)
      c[j] < c[k] ? j = i : j++;
    while (i <= j) i += k - j;
  }
  while (m > 2 && q > 1) {
    memcpy(x + y, c + o[q - 1], o[q] - o[q - 1]), y += o[q] - o[q - 1];
    if (q--, p[q] != 1 || p[q + 1] != 1) m--;
  }
  if (q == 1)
    memcpy(x + y, c + o[q - 1], o[q] - o[q - 1]), y += o[q] - o[q - 1];
  else if (m == 1)
    check(c, c + n, n) ? memcpy(x + y, c, o[q]),
        y += o[q]      : (copy(x + y, c + n, n), 0);
  else {
    for (n = o[q], i = 0, j = 1, k = 0; j < n; c[mod(j + k)] < c[mod(i + k)]
                                               ? i += k + 1,
        j = j <= i ? i + 1 : j : j += k + 1, k = 0)
      while (c[mod(i + k)] == c[mod(j + k)]) k++;
    i = i == n << 1 ? 0 : i, memcpy(x + y, c + i, n - i),
    memcpy(x + y + n - i, c, i);
    for (i = q; --i && !strncmp(c + o[i - 1], c + o[i], n - o[i]) &&
                check(c + n - o[i] + o[i - 1], c + o[i], o[i] - o[i - 1]);)
      ;
    if ((j = strncmp(c + o[i], x + y, n - o[i])) < 0 ||
        !j && !check(x + y + n - o[i], c + o[i], o[i]))
      memcpy(x + y, c + o[i], n - o[i]), copy(x + y + n - o[i], c + o[i], o[i]);
    for (i = k = 1, j = 0; i < n; k < i + a[i] ? k = i + a[j = i] : 0, i++) {
      if (i < k) a[i] = min(k - i, a[i - j]);
      while (c[i + a[i]] == c[a[i]]) a[i]++;
    }
    for (i = 0, j = n - 1; i < j; i++, j--)
      a[i] ^= a[j] ^= a[i] ^= a[j], c[i] ^= c[j] ^= c[i] ^= c[j];
    if (strncmp(c, x + y, n) < 0) memcpy(x + y, c, n);
    for (i = k = 1, j = 0; i < n; k < i + b[i] ? k = i + b[j = i] : 0, i++) {
      if (i < k) b[i] = min(k - i, a[n - 1 - j + i]);
      while (c[i + b[i]] == c[n - 1 - b[i]]) b[i]++;
    }
    for (i = 2, j = 1; i < n; i++)
      if (b[j] < i - j && c[j + b[j]] < c[n - 1 - b[j]] ||
          i - j <= b[j] && a[n - 1 - i + j] < n - i &&
              c[n - 1 - a[n - 1 - i + j]] < c[n - 1 - i + j - a[n - 1 - i + j]])
        j = i;
    if ((i = strncmp(c, x + y, j)) < 0 || !i && !check(x + y + j, c + n, n - j))
      memcpy(x + y, c, j), copy(x + y + j, c + n, n - j);
  }
  return 0 * printf("%s\n", x);
}
