#include <bits/stdc++.h>
using namespace std;
void update(char *answ, char *slov, int n) {
  bool kt = 0;
  for (int i = 0; i < n; ++i)
    if (answ[i] != slov[i]) {
      if (answ[i] > slov[i]) kt = 1;
      break;
    }
  if (kt) {
    copy(slov, slov + n, answ);
  }
}
void duval(char *slov, int n, int *pun, bool *fact, int &m, bool type) {
  m = 0, pun[0] = -1;
  int i = 0, j = 0, k = 1;
  bool kt = 0;
  while (i < n) {
    if (slov[k] == slov[j]) {
      ++j, ++k;
    } else if (slov[k] > slov[j]) {
      j = i, ++k;
    } else {
      int t = k - j;
      if (t == 1) {
        if (!kt || type) ++m;
        while (i <= j) {
          pun[m] = i + t - 1;
          i += t;
        }
        kt = fact[m] = 1;
      } else {
        ++m;
        while (i <= j) {
          pun[m] = i + t - 1;
          i += t;
        }
        kt = fact[m] = 0;
      }
      j = i, k = j + 1;
    }
  }
}
int nex[5000050];
int extend[5000050];
void getnext(char *Take, int money) {
  nex[0] = money;
  while (nex[1] < money && Take[nex[1]] == Take[1 + nex[1]]) {
    ++nex[1];
  }
  int pos = 1, P = nex[1];
  for (int i = 2; i < money; ++i) {
    int len = nex[i - pos];
    if (i + len - 1 < P) {
      nex[i] = len;
    } else {
      nex[i] = max(0, P - i + 1);
      while (i + nex[i] < money && Take[nex[i]] == Take[i + nex[i]]) {
        ++nex[i];
      }
      pos = i, P = i + nex[i] - 1;
    }
  }
}
void exKMP(char *Shire, int n, char *Take, int money) {
  getnext(Take, money);
  while (extend[0] < money && extend[0] < n &&
         Shire[extend[0]] == Take[extend[0]]) {
    ++extend[0];
  }
  int pos = 0, P = extend[0] - 1;
  for (int i = 1; i < n; ++i) {
    int len = nex[i - pos];
    if (i + len - 1 < P) {
      extend[i] = len;
    } else {
      extend[i] = max(0, P - i + 1);
      while (extend[i] < money && i + extend[i] < n &&
             Shire[i + extend[i]] == Take[extend[i]]) {
        ++extend[i];
      }
      pos = i, P = i + extend[i] - 1;
    }
  }
}
int pun[5000050], money;
bool fact[5000050];
void solve(char *slov, char *sirena, int n, char *answ) {
  copy(slov, slov + n, answ);
  exKMP(slov, n, sirena, n);
  int k = 0;
  for (int i = 1; i < n; ++i) {
    if (extend[k] < i - k) {
      if (sirena[extend[k]] > slov[k + extend[k]]) {
        k = i;
      }
    } else if (nex[i - k] < n - i) {
      if (sirena[i - k + nex[i - k]] > sirena[nex[i - k]]) {
        k = i;
      }
    }
  }
  reverse(slov + k, slov + n);
  update(answ, slov, n);
  reverse(slov + k, slov + n);
  duval(sirena, n, pun, fact, money, 1);
  reverse(slov, slov + n - pun[money - 1] - 1);
  update(answ, slov, n);
  reverse(slov + n - pun[money - 1] - 1, slov + n);
  update(answ, slov, n);
  reverse(slov + n - pun[money - 1] - 1, slov + n);
  reverse(slov, slov + n - pun[money - 1] - 1);
  reverse(slov, slov + n - pun[money - 2] - 1);
  update(answ, slov, n);
  reverse(slov + n - pun[money - 2] - 1, slov + n);
  update(answ, slov, n);
}
int n, k;
char slov[5000050];
char t[5000050];
char answ[5000050];
char sirena[5000050];
int main() {
  scanf("%s", slov);
  scanf("%d", &k);
  n = strlen(slov);
  copy(slov, slov + n, answ);
  reverse_copy(slov, slov + n, sirena);
  update(answ, sirena, n);
  if (k >= 2) {
    duval(sirena, n, pun, fact, money, 0);
    if (money <= k) {
      for (int i = 1; i <= money; ++i) {
        if (!fact[i]) reverse(slov + n - pun[i] - 1, slov + n - pun[i - 1] - 1);
      }
      update(answ, slov, n);
    } else {
      for (int i = 0; i < k - 2; ++i) {
        if (!fact[money - i])
          reverse(slov + n - pun[money - i] - 1,
                  slov + n - pun[money - i - 1] - 1);
      }
      int l = pun[money - (k - 2)] + 1;
      solve(slov + n - l, sirena, l, t);
      for (int i = 0; i < l; ++i) {
        slov[n - l + i] = t[i];
      }
      update(answ, slov, n);
    }
  }
  printf("%s\n", answ);
  return 0;
}
