#include <bits/stdc++.h>
using namespace std;
string s;
int n, K;
vector<string> lyndon;
inline string rev(string s) {
  reverse(s.begin(), s.end());
  return s;
}
inline string minstr(string s) { return min(s, rev(s)); }
int main() {
  cin.sync_with_stdio(0);
  cin >> s >> K;
  if (K == 1) {
    string t = s;
    reverse(t.begin(), t.end());
    cout << min(s, t);
    return 0;
  }
  reverse(s.begin(), s.end());
  n = s.size();
  int a = 0;
  while (a < n) {
    int b = a + 1, c = a;
    while (b < n && s[c] <= s[b]) {
      if (s[b] == s[c])
        b++, c++;
      else
        b++, c = a;
    }
    int st = a;
    while (a <= c) a += (b - c);
    if (b - c != 1)
      lyndon.push_back(s.substr(st, a - st));
    else {
      string ch = string({s[st]});
      for (int i = 0; i < (a - st) / (b - c); i++) lyndon.push_back(ch);
    }
  }
  reverse(s.begin(), s.end());
  vector<pair<bool, string> > division;
  bool lst1 = 0;
  for (auto &x : lyndon) {
    if (x.size() == 1) {
      if (lst1)
        division.back().second += x;
      else
        division.push_back(make_pair(1, x));
      lst1 = 1;
    } else {
      division.push_back(make_pair(0, x));
      lst1 = 0;
    }
  }
  if (division.size() <= K) {
    string res;
    while (!division.empty()) {
      if (division.back().first)
        reverse(division.back().second.begin(), division.back().second.end());
      res += division.back().second;
      division.pop_back();
    }
    cout << res;
  } else {
    string res;
    while (K > 2) {
      if (division.back().first)
        reverse(division.back().second.begin(), division.back().second.end());
      res += division.back().second;
      division.pop_back();
      K--;
    }
    int sl = 0;
    for (auto &x : division) sl += x.second.size();
    s = s.substr(s.size() - sl, sl);
    string rs = s;
    reverse(rs.begin(), rs.end());
    string ans;
    string s1 = minstr(division.back().second) +
                minstr(s.substr(division.back().second.size(),
                                sl - division.back().second.size()));
    string s2 =
        minstr(division[division.size() - 2].second + division.back().second) +
        minstr(s.substr(division.back().second.size() +
                            division[division.size() - 2].second.size(),
                        sl - division.back().second.size() -
                            division[division.size() - 2].second.size()));
    ans = min(minstr(s), min(s1, s2));
    string cs = rs + "#" + s;
    vector<int> lcp(cs.size());
    int mid = 0, r = 0;
    for (int i = 1; i < cs.size(); i++) {
      if (i < r)
        lcp[i] = min(lcp[i - mid], r - i);
      else
        lcp[i] = 0;
      while (i + lcp[i] < cs.size() && cs[lcp[i]] == cs[i + lcp[i]]) lcp[i]++;
      if (i + lcp[i] - 1 > r) r = i + lcp[i] - 1, mid = i;
    }
    int p1 = 1;
    for (int i = 2; i < s.size(); i++) {
      int d1 = i - p1;
      int l1 = lcp[p1 + s.size() + 1];
      if (l1 != d1) {
        if (rs[l1] > s[p1 + l1]) p1 = i;
      } else {
        int l2 = lcp[d1];
        if (l2 < s.size() - i) {
          if (rs[l2] < rs[d1 + l2]) p1 = i;
        }
      }
    }
    ans = min(ans, s.substr(0, p1) + rs.substr(0, s.size() - p1));
    cout << res + ans;
  }
  return 0;
}
