594_E. Cutting the Line

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

Example input:
aba
2

bababa
1

abacabadabacaba
4

aaaabacaba
2


Example output:
aab

ababab

aababacabacabad

aaaaabacab


Tags: string suffix structures
strings
Difficulty rating: 3100

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 10000005;
int n, m;
char s[N], ans[N];
int p[N], q[N], tot;
void getmn() {
  for (int i = 0; i < n;) {
    int j = i, k = i + 1;
    for (; k < n && s[j] <= s[k]; ++k) j = (s[j] < s[k] ? i : j + 1);
    p[tot] = i;
    q[tot++] = k - j;
    for (; i <= j; i += k - j)
      ;
  }
}
int z[N];
void init() {
  z[0] = 2 * n + 1;
  for (int i = 1, j = 0, r = 0; i <= 2 * n; i++) {
    z[i] = max(min(r - i, z[i - j]), 0);
    for (; s[z[i]] == s[i + z[i]]; z[i]++)
      ;
    if (i + z[i] > r) r = i + z[j = i];
  }
}
void solve(char *res, int len) {
  int k = 2 * n + 1 - len, t = z[k];
  strncpy(res, s[t] < s[k + t] ? s : (s + k), len);
}
int main() {
  scanf("%s%d", s, &m);
  int l = n = strlen(s);
  reverse(s, s + n);
  reverse_copy(s, s + n, s + n + 1);
  s[n] = '#';
  init();
  getmn();
  for (; tot && m > 2; m--) {
    tot--;
    strncpy(ans + n - l, s + p[tot], l - p[tot]);
    l = p[tot];
    if (q[tot] == 1)
      for (; tot && q[tot - 1] == 1; l = p[--tot])
        strncpy(ans + n - l, s + p[tot - 1], l - p[tot - 1]);
  }
  if (m == 1 || tot < 2)
    solve(ans + n - l, l);
  else {
    static char tmp[N];
    strcpy(tmp, ans);
    strncpy(ans + n - l, s + p[tot - 1], l - p[tot - 1]);
    strncpy(tmp + n - l, s + p[tot - 2], l - p[tot - 2]);
    solve(ans + n - p[tot - 1], p[tot - 1]);
    solve(tmp + n - p[tot - 2], p[tot - 2]);
    if (strcmp(tmp, ans) < 0) strcpy(ans, tmp);
    int k = 0;
    for (int i = 1; i < l; i++) {
      int a = 2 * n + 1 - l + k, t = z[a];
      if (t < i - k) {
        if (s[a + t] < s[t]) k = i;
      } else {
        t = z[i - k];
        if (s[t] < s[i - k + t]) k = i;
      }
    }
    strncpy(tmp + n - l, s + 2 * n + 1 - l, k);
    strncpy(tmp + n - l + k, s, l - k);
    if (strcmp(tmp, ans) < 0) strcpy(ans, tmp);
  }
  puts(ans);
}