#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T>
inline void print(T first) {
  if (first < 0) {
    putchar('-');
    return print(-first);
  }
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
char s[10000011], ans[10000011], tmp[10000011], *cur, *t;
int z[10000011], L[10000011], n;
void func(int n) {
  z[0] = 0;
  for (int i = 1, l = 0, r = 0; i < n; i++) {
    if (i <= r) z[i] = min(z[i - l], r - i + 1);
    while (i + z[i] < n && s[i + z[i]] == s[z[i]]) z[i]++;
    if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
  }
}
int Lyndon(char *s, int n) {
  int r = 0;
  for (int i = 0, j, k; i < n;) {
    for (k = i, j = i + 1; j < n and s[k] <= s[j]; j++)
      s[k] < s[j] ? k = i : ++k;
    for (L[r++] = i; i <= k; i += j - k)
      ;
  }
  return r;
}
void add(char *s, int n, char *&d) {
  copy(s, s + n, d);
  d += n;
}
void get(int ed, char *d) {
  int m = z[2 * n - ed];
  add((m >= ed or s[m] < s[ed - 1 - m]) ? s : s + 2 * n - ed, ed, d);
}
void print(char *s, int n) {
  for (int i = 0; i < n; i++) putchar(s[i]);
  puts("");
}
int main() {
  int k, m, l;
  scanf("%s", s);
  cin >> k;
  n = strlen(s);
  for (int i = 0; i < n; i++) s[i + n] = s[i];
  reverse(s, s + n);
  m = Lyndon(s, n);
  L[m--] = l = n;
  func(n << 1);
  cur = ans;
  if (k > 2) {
    while (true) {
      if (s[l - 1] != s[L[m]]) break;
      l = L[m--];
      if (m < 0) break;
    }
    if (l != n) add(s + n, n - l, cur), k--;
  }
  while (true) {
    if (k <= 2 or m < 0) break;
    add(s + L[m], l - L[m], cur);
    l = L[m];
    m--;
    k--;
  }
  if (k == 1 or m <= 0) {
    get(l, cur);
  } else {
    t = tmp;
    add(s + L[m], l - L[m], cur);
    get(L[m], cur);
    add(ans, n - l, t);
    add(s + L[m - 1], l - L[m - 1], t);
    get(L[m - 1], t);
    if (strcmp(tmp, ans) < 0) copy(tmp, tmp + n, ans);
    int now = 1, len;
    for (int i = 2; i < l; i++) {
      len = z[2 * n - i];
      if (len < i - now) {
        if (s[len] < s[i - 1 - len]) now = i;
      } else {
        len = z[i - now];
        if (len <= now and s[i - now + len] < s[len]) now = i;
      }
    }
    t = tmp;
    add(ans, n - l, t);
    add(s + 2 * n - l, l - now, t);
    add(s, now, t);
    if (strcmp(tmp, ans) < 0)
      print(tmp, n);
    else
      print(ans, n);
    return 0;
  }
  print(ans, n);
  return 0;
}
