#include <bits/stdc++.h>
using namespace std;
string s, t, ts, ans;
int pos[10000005], len[10000005], tot;
inline string reverse(string s) {
  string t;
  t.resize(s.size());
  int n = s.size();
  for (int i = 0; i < n; i++) t[n - i - 1] = s[i];
  return t;
}
void Duval(const string& s) {
  int n = s.size();
  for (int i = 0; i < n;) {
    int j = i, k = i + 1;
    while (s[j] <= s[k]) {
      if (s[j] == s[k])
        ++j;
      else
        j = i;
      ++k;
    }
    len[++tot] = k - j;
    while (i <= j) {
      pos[tot] = i + k - j - 1;
      i += k - j;
    }
  }
}
int p[10000005];
void Exkmp(const string& s) {
  int n = s.size();
  int mid = 0, mx = 0;
  p[0] = n;
  for (int i = 1; i < n; i++) {
    if (i <= mx) p[i] = min(p[i - mid], mx - i + 1);
    while (s[i + p[i]] == s[p[i]]) ++p[i];
    if (i + p[i] - 1 > mx) mid = i, mx = i + p[i] - 1;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> s;
  t = reverse(s);
  int k;
  cin >> k;
  if (k == 1) return cout << min(s, t), 0;
  Duval(t);
  pos[0] = -1;
  while (k > 2 && tot) {
    if (len[tot] == 1) {
      while (tot && len[tot] == 1)
        ans += t.substr(pos[tot - 1] + 1, pos[tot] - pos[tot - 1]), --tot;
      --k;
    } else
      ans += t.substr(pos[tot - 1] + 1, pos[tot] - pos[tot - 1]), --k, --tot;
  }
  if (tot == 0) return cout << ans, 0;
  if (tot == 1) {
    string tmp = t.substr(0, pos[1] + 1);
    tmp = min(tmp, reverse(tmp));
    cout << ans + tmp;
    return 0;
  }
  s = reverse(t = t.substr(0, pos[tot] + 1));
  ts = t + "#" + s;
  Exkmp(ts);
  string tmp = min(s, t);
  int cut = 0, n = s.size();
  for (int i = 1; i < n; i++) {
    int cl = min(i - cut, p[n + 1 + cut]);
    if (cut + cl == i) cl += p[cl];
    cl = min(cl, n - cut);
    if ((cl < i - cut ? s[cut + cl] : t[cut + cl - i]) < t[cl]) cut = i;
  }
  tmp = min(tmp, s.substr(0, cut) + t.substr(0, n - cut));
  string las = t.substr(pos[tot - 1] + 1, len[tot]);
  string lass = t.substr(pos[tot - 2] + 1, len[tot]);
  int st = pos[tot - 1] + 1;
  string tt = t.substr(st, n - st);
  string res = t.substr(0, n - tt.size());
  tt += min(res, reverse(res));
  tmp = min(tmp, tt);
  if (las == lass) {
    st = pos[tot - 2] + 1;
    tt = t.substr(st, n - st);
    res = t.substr(0, n - tt.size());
    tt = tt + min(res, reverse(res));
    tmp = min(tmp, tt);
  }
  cout << ans + tmp;
  return 0;
}
