#include <bits/stdc++.h>
using namespace std;
char s[5000005], sr[5000005], srs[2 * 5000005], ans[5000005], a[5000005],
    b[5000005];
char *ans1 = a, *tmp = b;
int bst, N, u, hd, n, i, kp, k, j, pre, tot = 0, seq[5000005], bx[2 * 5000005],
                                        ty[2 * 5000005], alr = 0;
void output(char *pos, int x) {
  for (int i = 0; i < x; i++) printf("%c", *(pos + i));
}
char *mymin(char *x, char *y, int l) {
  int i;
  for (i = 0; i < l; i++)
    if ((*(x + i)) != (*(y + i))) break;
  if (*(x + i) < *(y + i)) return x;
  return y;
}
void in(char *pos1, char *pos2, int x) {
  for (int i = 0; i < x; i++) *(pos1 + i) = *(pos2 + i);
}
void inrev(char *pos1, char *pos2, int x) {
  for (int i = 0; i < x; i++) *(pos1 + i) = *(pos2 - i);
}
int len(int p) { return seq[p] - seq[p - 1]; }
int cmp(char *pos1, char *pos2, int x) {
  int i;
  for (i = 0; i < x; i++)
    if ((*(pos1 + i)) != (*(pos2 + i))) break;
  if (i == x) return 0;
  if (*(pos1 + i) < *(pos2 + i)) return -1;
  return 1;
}
int main() {
  scanf("%s", s + 1);
  n = N = strlen(s + 1);
  for (i = 1; i <= n; i++) sr[n - i + 1] = s[i];
  scanf("%d", &kp);
  if (kp == 1) {
    output(mymin(s + 1, sr + 1, n), n);
    return 0;
  }
  for (i = 1; i <= n;) {
    j = i;
    k = i + 1;
    while (k <= n && sr[k] >= sr[j]) {
      if (sr[k] > sr[j])
        j = i;
      else
        j++;
      k++;
    }
    while (i <= j) {
      seq[++tot] = i + k - j - 1;
      i += k - j;
    }
  }
  hd = tot;
  while (kp > 2 && hd) {
    pre = hd;
    if (len(hd) == 1) {
      while (hd && len(hd) == 1) hd--;
      inrev(ans + alr + 1, sr + seq[pre], seq[pre] - seq[hd]);
      alr += seq[pre] - seq[hd];
    } else {
      while (hd && len(hd) == len(pre) &&
             cmp(sr + seq[hd - 1] + 1, sr + seq[pre - 1] + 1, len(pre)) == 0)
        hd--;
      in(ans + alr + 1, sr + seq[hd] + 1, seq[pre] - seq[hd]);
      alr += seq[pre] - seq[hd];
    }
    kp--;
  }
  output(ans + 1, alr);
  if (hd == 0) return 0;
  n = seq[hd];
  for (i = 1; i <= n; i++) sr[n + i] = sr[i];
  i = j = 1;
  while (j < n) {
    for (k = 0; k < n; k++)
      if (sr[i + k] != sr[j + 1 + k]) break;
    if (k == n) {
      in(ans1 + 1, sr + i, n);
      goto tag;
    }
    if (sr[i + k] < sr[j + 1 + k])
      j = j + 1 + k;
    else
      i = max(j + 1, i + k + 1), j = i;
  }
  in(ans1 + 1, sr + i, n);
tag:;
  for (i = 1; i <= n; i++) srs[i] = srs[n + n - i + 1] = sr[i];
  j = k = 1;
  for (i = 2; i <= 2 * n; i++) {
    if (i <= k && bx[i - j + 1] < k - j + 1)
      bx[i] = j - 1 + bx[i - j + 1];
    else {
      j = i;
      bx[i] = max(k, i - 1);
      while (bx[i] < 2 * n && srs[bx[i] + 1] == srs[bx[i] + 2 - i])
        bx[i]++, k++;
      if (bx[i] == 2 * n || srs[bx[i] + 1] > srs[bx[i] + 2 - i])
        ty[i] = 1;
      else
        ty[i] = 0;
    }
  }
  bst = 1;
  for (i = 2; i <= n; i++) {
    if (bx[n + bst + 1] < n + i) {
      if (ty[n + bst + 1] == 0) bst = i;
      continue;
    }
    if (ty[i - bst + 1] == 1) bst = i;
  }
  inrev(tmp + 1, sr + n, bst);
  in(tmp + bst + 1, sr + 1, n - bst);
  if (cmp(ans1 + 1, tmp + 1, n) == 1) swap(ans1, tmp);
  while (hd > 0) {
    pre = hd;
    for (i = hd; i > 0 && len(i) == len(pre) &&
                 cmp(sr + seq[i - 1] + 1, sr + seq[pre - 1] + 1, len(pre)) == 0;
         i--)
      ;
    hd = i;
    if (seq[pre] - seq[hd] <= n - seq[pre]) break;
    if (cmp(sr + seq[hd] + 1, sr + seq[pre] + 1, n - seq[pre]) != 0) break;
    if (cmp(sr + seq[hd] + n - seq[pre] + 1, s + N - seq[pre] + 1,
            seq[pre] - seq[hd]) == 1)
      break;
  }
  in(tmp + 1, sr + seq[pre] + 1, n - seq[pre]);
  inrev(tmp + n - seq[pre] + 1, sr + seq[pre], seq[pre]);
  if (cmp(ans1 + 1, tmp + 1, n) == 1) swap(ans1, tmp);
  output(ans1 + 1, n);
  return 0;
}
