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;
vector<pair<int, int> > lyndon(string s) {
  int n = int(s.length());
  vector<pair<int, int> > ret;
  for (int i = 0; i < n;) {
    int j = i, k = i + 1;
    while (k < n && s[j] <= s[k]) {
      if (s[j] < s[k])
        j = i;
      else
        ++j;
      ++k;
    }
    ret.push_back(make_pair(i, k - j));
    while (i <= j) i += k - j;
  }
  return ret;
}
vector<int> z_algorithm(string s) {
  int n = int(s.length()), l = 0, r = 0;
  vector<int> f(n);
  f[0] = n;
  for (int i = 1; i < n; i++) {
    if (i < r) f[i] = min(f[i - l], r - i);
    while (s[i + f[i]] == s[f[i]]) ++f[i];
    if (i + f[i] > r) r = i + f[i], l = i;
  }
  return f;
}
string min_cyc(string s) {
  int n = int(s.length());
  s += s;
  vector<pair<int, int> > l = lyndon(s);
  for (int i = 0; i < int(l.size()); i++)
    if (i == int(l.size()) - 1 || l[i + 1].first >= n)
      return s.substr(l[i].first, n);
}
string rev(string s) {
  reverse(s.begin(), s.end());
  return s;
}
int k;
string s;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> s >> k;
  reverse(s.begin(), s.end());
  vector<pair<int, int> > l = lyndon(s);
  string ans;
  while (!l.empty() && k > 2) {
    if (l.back().second > 1) {
      auto x = l.back();
      l.pop_back();
      ans += s.substr(x.first, int(s.length()) - x.first),
          s.erase(s.begin() + x.first, s.end());
    } else {
      while (!l.empty()) {
        auto x = l.back();
        if (x.second > 1) break;
        l.pop_back();
        ans += s.substr(x.first, int(s.length()) - x.first),
            s.erase(s.begin() + x.first, s.end());
      }
    }
    --k;
  }
  if (l.empty()) return cout << ans, 0;
  if (k == 1) return cout << ans + min(s, rev(s)), 0;
  string tmp = min(s, min(rev(s), min_cyc(s)));
  vector<int> z = z_algorithm(s + rev(s));
  int n = int(s.length()), p = 0;
  auto cmp = [&](int x, int y) {
    int lcp = min(y - x, z[n + n - y - 1]);
    if (lcp < y - x) return s[y - lcp] < s[lcp];
    lcp = min(x + 1, z[y - x]);
    if (lcp < x + 1) return s[lcp] < s[y - x + lcp];
    return false;
  };
  for (int i = 1; i < n - 1; i++)
    if (!cmp(p, i)) p = i;
  tmp = min(tmp, rev(s.substr(p + 1, n - p - 1)) + s.substr(0, p + 1));
  l = lyndon(s);
  p = l.back().first;
  auto cmp2 = [&](int x, int y) {
    for (int i = x + n - y, j = y - 1; i < n; i++, j--)
      if (s[i] != s[j]) return s[i] < s[j];
    return false;
  };
  for (int i = int(l.size()) - 2; ~i; i--) {
    if (2 * l[i + 1].second > l[i].second) break;
    if (cmp2(l[i].first, l[i + 1].first)) p = l[i].first;
  }
  tmp = min(tmp, s.substr(p, n - p) + rev(s.substr(0, p)));
  cout << ans + tmp;
  return 0;
}