Problem: You are given a non-empty line s and an integer k. The following operation is performed with this line exactly once:

  * A line is split into at most k non-empty substrings, i.e. string s is represented as a concatenation of a set of strings s = t1 + t2 + ... + tm, 1 ≤ m ≤ k. 
  * Some of strings ti are replaced by strings tir, that is, their record from right to left. 
  * The lines are concatenated back in the same order, we get string s' = t'1t'2... t'm, where t'i equals ti or tir. 



Your task is to determine the lexicographically smallest string that could be the result of applying the given operation to the string s.

Input

The first line of the input contains string s (1 ≤ |s| ≤ 5 000 000), consisting of lowercase English letters. The second line contains integer k (1 ≤ k ≤ |s|) — the maximum number of parts in the partition.

Output

In the single line print the lexicographically minimum string s' which can be obtained as a result of performing the described operation. 

Examples

Input

aba
2


Output

aab


Input

aaaabacaba
2


Output

aaaaabacab


Input

bababa
1


Output

ababab


Input

abacabadabacaba
4


Output

aababacabacabad

Solution (in CPP):

Solution:
#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;
}