#include <bits/stdc++.h>
using namespace std;
template <typename T>
int duval(const T* s, int n, int* d) {
  int r = 0;
  for (int i = 0, j, k; i < n;) {
    for (k = i, j = i + 1; j < n && s[k] <= s[j]; j++)
      s[k] < s[j] ? k = i : ++k;
    for (d[r++] = i; i <= k; i += j - k)
      ;
  }
  return r;
}
char s[10010000], ans[10010000], ss[10010000];
int d[10010000], z[10010000];
void makez(int n) {
  int L = 0, R = 0;
  z[0] = n;
  for (int i = 1; i < n; i++) {
    if (i > R) {
      L = R = i;
      while (R < n && s[R - L] == s[R]) R++;
      z[i] = R - L;
      R--;
    } else {
      int k = i - L;
      if (z[k] < R - i + 1)
        z[i] = z[k];
      else {
        L = i;
        while (R < n && s[R - L] == s[R]) R++;
        z[i] = R - L;
        R--;
      }
    }
  }
  return;
}
int n;
char* cur = ans;
void add(const char* src, int l, char*& dst) {
  copy(src, src + l, dst);
  dst += l;
  return;
}
void add_suf(int l, char* dst) {
  int m = z[2 * n - l];
  add((m >= l || s[m] < s[l - 1 - m]) ? s : s + 2 * n - l, l, dst);
  return;
}
int main() {
  int m, c, K, l, best;
  scanf("%s", s);
  n = strlen(s);
  copy(s, s + n, s + n);
  reverse(s, s + n);
  scanf("%d", &K);
  m = duval(s, n, d) - 1;
  makez(2 * n);
  l = n;
  if (K > 2) {
    while (true) {
      bool ok = false;
      if (s[l - 1] == s[d[m]]) ok = true;
      if (!ok) break;
      m--;
      l = d[m + 1];
      if (m < 0) break;
    }
    if (l != n) {
      add(s + n, n - l, cur);
      K--;
    }
  }
  while (true) {
    if (K <= 2) break;
    if (m < 0) break;
    add(s + d[m], l - d[m], cur);
    l = d[m];
    m--;
    K--;
  }
  if (K == 1 || m <= 0) {
    add_suf(l, cur);
  } else {
    char* t = ss;
    add(s + d[m], l - d[m], cur);
    add_suf(d[m], cur);
    add(ans, n - l, t);
    add(s + d[m - 1], l - d[m - 1], t);
    add_suf(d[m - 1], t);
    if (strcmp(ss, ans) < 0) copy(ss, ss + n, ans);
    best = 1;
    for (int i = 2; i < l; i++) {
      c = z[2 * n - i];
      if (c < i - best) {
        if (s[c] < s[i - 1 - c]) best = i;
      } else {
        c = z[i - best];
        if (c <= best && s[i - best + c] < s[c]) best = i;
      }
    }
    t = ss;
    add(ans, n - l, t);
    add(s + 2 * n - l, l - best, t);
    add(s, best, t);
    ss[n] = 0;
    if (strcmp(ss, ans) < 0) strcpy(ans, ss);
  }
  puts(ans);
  return 0;
}
