#include <bits/stdc++.h>
using namespace std;
struct pat {
  int x, y;
  pat(int x = 0, int y = 0) : x(x), y(y) {}
  bool operator<(const pat &p) const { return x == p.x ? y < p.y : x < p.x; }
};
namespace RKK {
const int N = 5000011;
char str[N], srt[N << 1], prt[N];
int n, kap;
void cmp(char *s) {
  for (int i = 1; i <= n; i++) {
    if (s[i] > prt[i])
      return;
    else if (s[i] < prt[i])
      break;
  }
  memcpy(prt, s, (n + 1) * sizeof(char));
}
int ep[N], T[N], cnt;
void lyndon() {
  int i = 1, j, k;
  while (i <= n) {
    for (j = i, k = i + 1; k <= n && str[k] >= str[j]; k++)
      str[k] > str[j] ? j = i : j++;
    while (i <= j) i += k - j;
    cnt++, ep[cnt] = i - 1, T[cnt] = k - j;
  }
}
int ex[N], p[N];
void exkmp(char *s, int n, char *t, int m) {
  ex[1] = m;
  for (int i = 2, l = 0, r = 0; i <= m; i++) {
    if (i <= r) ex[i] = min(ex[i - l + 1], r - i + 1);
    while (i + ex[i] <= m && t[i + ex[i]] == t[1 + ex[i]]) ex[i]++;
    if (i + ex[i] - 1 > r) l = i, r = i + ex[i] - 1;
  }
  for (int i = 1, l = 0, r = 0; i <= n; i++) {
    if (i <= r) p[i] = min(ex[i - l + 1], r - i + 1);
    while (i + p[i] <= n && s[i + p[i]] == t[1 + p[i]]) p[i]++;
    if (i + p[i] - 1 > r) l = i, r = i + p[i] - 1;
  }
}
void work1() {
  reverse(str + 1, str + 1 + n);
  cmp(str);
  reverse(str + 1, str + 1 + n);
}
void work2() {
  for (int i = 1; i <= n; i++) srt[i] = srt[i + n] = str[i];
  int i = 1, j, k, lst, m = n << 1;
  while (i <= n) {
    lst = i - 1;
    for (j = i, k = i + 1; k <= m && srt[k] >= srt[j]; k++)
      srt[k] > srt[j] ? j = i : j++;
    while (i <= j) i += k - j;
  }
  cmp(srt + lst);
}
void work3() {
  for (int i = 1; i <= n; i++) srt[i] = str[i];
  reverse(srt + 1, srt + 1 + n);
  exkmp(srt, n, str, n);
  reverse(p + 1, p + 1 + n);
  int o = n;
  for (int i = n - 1; i; i--) {
    if (p[o - 1] < o - i) {
      if (str[o - 1 - p[o - 1]] < str[p[o - 1] + 1]) o = i;
    } else if (ex[o - i + 1] < i - 1) {
      if (str[ex[o - i + 1] + 1] < str[o - i + 1 + ex[o - i + 1]]) o = i;
    }
  }
  reverse(srt + 1, srt + 1 + n), reverse(srt + 1, srt + o),
      reverse(srt + 1, srt + 1 + n);
  cmp(srt);
}
void work4() {
  int x = cnt + 1, y, flag = 0;
  while (--x > 1) {
    for (int i = ep[x - 1] + 1, j = ep[x - 2] + 1; i <= ep[x]; i++, j++)
      if (str[i] != str[j]) {
        flag = 1;
        break;
      }
    if (flag) break;
  }
  y = x;
  while (++y <= cnt) {
    for (int i = ep[y - 1], j = ep[y - 2] + n - i + 1; i > ep[y - 2];
         i--, j++) {
      if (str[i] < str[j]) x = y;
      if (str[j] != str[i]) break;
    }
  }
  reverse(str + 1 + ep[x - 1], str + 1 + n);
  reverse(str + 1, str + 1 + n);
  cmp(str);
}
int main() {
  scanf("%s%d", str + 1, &kap), n = strlen(str + 1);
  if (kap == 1) {
    for (int i = 1, j = n; i <= n; i++, j--) {
      if (str[i] < str[j])
        break;
      else if (str[j] < str[i]) {
        reverse(str + 1, str + 1 + n);
        break;
      }
    }
    printf("%s\n", str + 1);
    return 0;
  }
  reverse(str + 1, str + 1 + n);
  lyndon();
  while (cnt && kap > 2) {
    for (int i = ep[cnt - 1] + 1; i <= ep[cnt]; i++) putchar(str[i]);
    if (T[cnt] != 1 || T[cnt - 1] != 1) kap--;
    cnt--;
  }
  n = ep[cnt];
  if (n) {
    memcpy(prt, str, (n + 1) * sizeof(char));
    work1(), work2(), work3(), work4();
  }
  for (int i = 1; i <= n; i++) putchar(prt[i]);
  return 0;
}
}  // namespace RKK
int main() { return RKK::main(); }
