#include <bits/stdc++.h>
using namespace std;
int n, m, k, i, j, z[10000005];
string st, sr, stt;
vector<int> pos, len;
void lyndon(int n) {
  int i, j, k;
  for (i = 1; i <= n;) {
    j = i;
    k = i + 1;
    while (k <= n && sr[j] <= sr[k]) {
      if (sr[j] == sr[k])
        j++;
      else
        j = i;
      k++;
    }
    pos.push_back(i);
    len.push_back(k - j);
    while (i + (k - j) <= k) i += k - j;
  }
  pos.push_back(n + 1);
}
void getz(int n) {
  int i, l = 0, r = 0;
  for (i = 2; i <= n; i++) {
    if (i < r) {
      z[i] = min(z[i - l + 1], r - i);
    }
    while (i + z[i] <= n && stt[z[i] + 1] == stt[i + z[i]]) z[i]++;
    if (i + z[i] > r) {
      l = i;
      r = i + z[i];
    }
  }
}
struct node {
  string ans;
  void add0(const string &st, int l, int len) {
    int i;
    for (i = 0; i < len; i++) {
      ans.push_back(st[l + i]);
    }
  }
  void add1(const string &st, int l, int len) {
    int i;
    for (i = len - 1; i >= 0; i--) {
      ans.push_back(st[l + i]);
    }
  }
  void add2(const string &st, int l, int len) {
    string tmp, tmp2;
    int i;
    for (i = 0; i < len; i++) {
      tmp.push_back(st[l + i]);
    }
    tmp2 = tmp;
    reverse(tmp2.begin(), tmp2.end());
    ans += min(tmp, tmp2);
  }
} ans1, ans2, ans3;
int main() {
  cin >> st >> k;
  n = st.size();
  st = " " + st;
  sr = st;
  reverse(sr.begin() + 1, sr.end());
  lyndon(n);
  stt = sr + st;
  st[n + 1] = '#';
  getz(n + n + 1);
  m = pos.size() - 2;
  while (k > 2 && m >= 0) {
    if (len[m] == 1) {
      while (m >= 0 && len[m] == 1) {
        ans1.add0(sr, pos[m], pos[m + 1] - pos[m]);
        m--;
      }
    } else {
      ans1.add0(sr, pos[m], pos[m + 1] - pos[m]);
      m--;
    }
    k--;
  }
  if (k == 1 || m < 1) {
    ans1.add2(sr, 1, pos[m + 1] - 1);
  } else {
    ans2 = ans1;
    ans3 = ans1;
    ans2.add0(sr, pos[m], pos[m + 1] - pos[m]);
    ans2.add2(sr, 1, pos[m] - 1);
    ans1.add0(sr, pos[m - 1], pos[m + 1] - pos[m - 1]);
    ans1.add2(sr, 1, pos[m - 1] - 1);
    if (ans2.ans < ans1.ans) ans1 = ans2;
    int p = n - pos[m + 1] + 2, q = p;
    for (i = p + 1; i <= n; i++) {
      char c1, c2;
      if (p + z[p + n + 1] < i) {
        c1 = st[p + z[p + n + 1]];
        c2 = sr[z[p + n + 1] + 1];
      } else {
        int h = min(z[i - p + 1], n - i);
        c1 = sr[h + 1];
        c2 = sr[h + (i - p + 1)];
      }
      if (c1 < c2) {
        p = i;
      }
    }
    ans3.add0(st, n - pos[m + 1] + 2, p - (n - pos[m + 1] + 2));
    ans3.add1(st, p, n - p + 1);
    if (ans3.ans < ans1.ans) ans1 = ans3;
  }
  cout << ans1.ans << endl;
  return 0;
}
