#include <bits/stdc++.h>
using namespace std;
namespace io {
const int SI = 1 << 21 | 1;
char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100];
int f, t;
inline void flush() { fwrite(OB, 1, OS - OB, stdout), OS = OB; }
inline void pc(char x) {
  *OS++ = x;
  if (OS == OT) flush();
}
template <class I>
inline void rd(I &x) {
  for (f = 1, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
                              IS == IT ? EOF : *IS++)
                            : *IS++);
       c < '0' || c > '9';
       c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
                       IS == IT ? EOF : *IS++)
                     : *IS++))
    if (c == '-') f = -1;
  for (x = 0; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + (c & 15),
      c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
                      IS == IT ? EOF : *IS++)
                    : *IS++))
    ;
  x *= f;
}
template <class I>
inline void rda(I *a, int &n) {
  for (int i = 1; i <= n; i++) rd(a[i]);
}
inline void rds(char *s, int &x) {
  for (c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
                       IS == IT ? EOF : *IS++)
                     : *IS++);
       c < 33 || c > 126;
       c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
                       IS == IT ? EOF : *IS++)
                     : *IS++))
    ;
  for (x = 0; c >= 33 && c <= 126;
       s[++x] = c, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
                                   IS == IT ? EOF : *IS++)
                                 : *IS++))
    ;
  s[x + 1] = '\0';
}
template <class I>
inline void print(I x, char k = '\n') {
  if (!x) pc('0');
  if (x < 0) pc('-'), x = -x;
  while (x) ch[++t] = x % 10 + '0', x /= 10;
  while (t) pc(ch[t--]);
  pc(k);
}
inline void prints(string s) {
  int x = s.length();
  while (t < x) pc(s[t++]);
  pc('\n'), t = 0;
}
struct Flush {
  ~Flush() { flush(); }
} flusher;
}  // namespace io
using io::print;
using io::prints;
using io::rd;
using io::rda;
using io::rds;
const int N = 5e6 + 7;
int n, k, r[N], l[N], t, z[N], p[N];
char s[N], ans[N], ss[N * 2];
inline void Lyndon(char *s, int n, int *r, int *l, int &t) {
  t = 0;
  int i = 1;
  while (i <= n) {
    int j = i, k = i + 1;
    while (k <= n && s[j] <= s[k]) j = s[j] == s[k++] ? j + 1 : i;
    while (i <= j) i += k - j;
    r[++t] = i - 1, l[t] = k - j;
  }
}
inline void k1() {
  for (int i = 1; i <= n; i++) {
    if (s[n + 1 - i] == s[i]) continue;
    if (s[n + 1 - i] < s[i]) reverse(s + 1, s + n + 1);
    break;
  }
  for (int i = 1; i <= n; i++) putchar(s[i]);
}
inline void k3() {
  for (int i = r[t - 1] + 1; i <= r[t]; i++) putchar(s[i]);
  k -= l[t] != 1 || l[t - 1] != 1, --t;
}
inline void upd(char *s) {
  for (int i = 1; i <= n; i++) {
    if (s[i] > ans[i]) return;
    if (s[i] < ans[i]) break;
  }
  for (int i = 1; i <= n; i++) ans[i] = s[i];
}
inline void upd1() {
  reverse(s + 1, s + n + 1);
  upd(s);
  reverse(s + 1, s + n + 1);
}
inline void upd2() {
  for (int i = 1; i <= n; i++) ss[i] = ss[i + n] = s[i];
  int i = 1, o;
  while (i <= n) {
    o = i;
    int j = i, k = i + 1;
    while (k <= n * 2 && ss[j] <= ss[k]) j = ss[j] == ss[k++] ? j + 1 : i;
    while (i <= j) i += k - j;
  }
  upd(ss + o - 1);
}
inline void Z(char *s, int n, int *z) {
  for (int i = 1; i <= n; i++) z[i] = 0;
  z[1] = n;
  for (int i = 2, l = 0, r = 0; i <= n; i++) {
    if (i <= r) z[i] = min(z[i - l + 1], r - i + 1);
    while (i + z[i] <= n && s[i + z[i]] == s[z[i] + 1]) ++z[i];
    if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
  }
}
inline void exkmp(char *s, int n, char *t, int m, int *z, int *p) {
  Z(t, m, z);
  for (int i = 1; i <= n; i++) p[i] = 0;
  for (int i = 1, l = 0, r = 0; i <= n; i++) {
    if (i <= r) p[i] = min(z[i - l + 1], r - i + 1);
    while (i + p[i] <= n && s[i + p[i]] == t[p[i] + 1]) ++p[i];
    if (i + p[i] - 1 > r) l = i, r = i + p[i] - 1;
  }
}
inline void upd3() {
  for (int i = 1; i <= n; i++) ss[i] = s[i];
  reverse(ss + 1, ss + n + 1);
  exkmp(ss, n, s, n, z, p);
  reverse(p + 1, p + n + 1);
  int j = n;
  for (int i = n - 1; i; i--)
    if (p[j - 1] < j - i) {
      if (s[j - 1 - p[j - 1]] < s[p[j - 1] + 1]) j = i;
    } else if (z[j - i + 1] < i - 1) {
      if (s[z[j - i + 1] + 1] < s[j - i + 1 + z[j - i + 1]]) j = i;
    }
  reverse(s + 1, s + j);
  reverse(s + 1, s + n + 1);
  upd(s);
  reverse(s + 1, s + n + 1);
  reverse(s + 1, s + j);
}
inline void upd4() {
  int p = t + 1;
  while (--p > 1) {
    bool ok = 1;
    for (int i = r[p - 1] + 1, j = r[p - 2] + 1; ok && i <= r[p]; i++, j++)
      if (s[i] != s[j]) ok = 0;
    if (!ok) break;
  }
  int q = p;
  while (++q <= t)
    for (int i = r[q - 1], j = r[q - 2] + 1 + n - i; i > r[q - 2]; i--, j++) {
      if (s[i] == s[j]) continue;
      if (s[i] < s[j]) p = q;
      break;
    }
  reverse(s + r[p - 1] + 1, s + n + 1);
  reverse(s + 1, s + n + 1);
  upd(s);
  reverse(s + 1, s + n + 1);
  reverse(s + r[p - 1] + 1, s + n + 1);
}
inline void k2() {
  for (int i = 1; i <= n; i++) ans[i] = s[i];
  upd1(), upd2(), upd3(), upd4();
}
int main() {
  rds(s, n), rd(k);
  if (k == 1) return k1(), 0;
  reverse(s + 1, s + n + 1);
  Lyndon(s, n, r, l, t);
  while (k >= 3 && t) k3();
  if ((n = r[t])) k2();
  for (int i = 1; i <= n; i++) putchar(ans[i]);
  return 0;
}
