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;
void update(char *answ, char *slov, int n) {
  bool kt = 0;
  for (int i = 0; i < n; ++i)
    if (answ[i] != slov[i]) {
      if (answ[i] > slov[i]) kt = 1;
      break;
    }
  if (kt) {
    copy(slov, slov + n, answ);
  }
}
void duval(char *slov, int n, int *pun, bool *fact, int &m, bool type) {
  m = 0, pun[0] = -1;
  int i = 0, j = 0, k = 1;
  bool kt = 0;
  while (i < n) {
    if (slov[k] == slov[j]) {
      ++j, ++k;
    } else if (slov[k] > slov[j]) {
      j = i, ++k;
    } else {
      int t = k - j;
      if (t == 1) {
        if (!kt || type) ++m;
        while (i <= j) {
          pun[m] = i + t - 1;
          i += t;
        }
        kt = fact[m] = 1;
      } else {
        ++m;
        while (i <= j) {
          pun[m] = i + t - 1;
          i += t;
        }
        kt = fact[m] = 0;
      }
      j = i, k = j + 1;
    }
  }
}
int nex[5000050];
int extend[5000050];
void getnext(char *Take, int money) {
  nex[0] = money;
  while (nex[1] < money && Take[nex[1]] == Take[1 + nex[1]]) {
    ++nex[1];
  }
  int pos = 1, P = nex[1];
  for (int i = 2; i < money; ++i) {
    int len = nex[i - pos];
    if (i + len - 1 < P) {
      nex[i] = len;
    } else {
      nex[i] = max(0, P - i + 1);
      while (i + nex[i] < money && Take[nex[i]] == Take[i + nex[i]]) {
        ++nex[i];
      }
      pos = i, P = i + nex[i] - 1;
    }
  }
}
void exKMP(char *Shire, int n, char *Take, int money) {
  getnext(Take, money);
  while (extend[0] < money && extend[0] < n &&
         Shire[extend[0]] == Take[extend[0]]) {
    ++extend[0];
  }
  int pos = 0, P = extend[0] - 1;
  for (int i = 1; i < n; ++i) {
    int len = nex[i - pos];
    if (i + len - 1 < P) {
      extend[i] = len;
    } else {
      extend[i] = max(0, P - i + 1);
      while (extend[i] < money && i + extend[i] < n &&
             Shire[i + extend[i]] == Take[extend[i]]) {
        ++extend[i];
      }
      pos = i, P = i + extend[i] - 1;
    }
  }
}
int pun[5000050], money;
bool fact[5000050];
void solve(char *slov, char *sirena, int n, char *answ) {
  copy(slov, slov + n, answ);
  exKMP(slov, n, sirena, n);
  int k = 0;
  for (int i = 1; i < n; ++i) {
    if (extend[k] < i - k) {
      if (sirena[extend[k]] > slov[k + extend[k]]) {
        k = i;
      }
    } else if (nex[i - k] < n - i) {
      if (sirena[i - k + nex[i - k]] > sirena[nex[i - k]]) {
        k = i;
      }
    }
  }
  reverse(slov + k, slov + n);
  update(answ, slov, n);
  reverse(slov + k, slov + n);
  duval(sirena, n, pun, fact, money, 1);
  reverse(slov, slov + n - pun[money - 1] - 1);
  update(answ, slov, n);
  reverse(slov + n - pun[money - 1] - 1, slov + n);
  update(answ, slov, n);
  reverse(slov + n - pun[money - 1] - 1, slov + n);
  reverse(slov, slov + n - pun[money - 1] - 1);
  reverse(slov, slov + n - pun[money - 2] - 1);
  update(answ, slov, n);
  reverse(slov + n - pun[money - 2] - 1, slov + n);
  update(answ, slov, n);
}
int n, k;
char slov[5000050];
char t[5000050];
char answ[5000050];
char sirena[5000050];
int main() {
  scanf("%s", slov);
  scanf("%d", &k);
  n = strlen(slov);
  copy(slov, slov + n, answ);
  reverse_copy(slov, slov + n, sirena);
  update(answ, sirena, n);
  if (k >= 2) {
    duval(sirena, n, pun, fact, money, 0);
    if (money <= k) {
      for (int i = 1; i <= money; ++i) {
        if (!fact[i]) reverse(slov + n - pun[i] - 1, slov + n - pun[i - 1] - 1);
      }
      update(answ, slov, n);
    } else {
      for (int i = 0; i < k - 2; ++i) {
        if (!fact[money - i])
          reverse(slov + n - pun[money - i] - 1,
                  slov + n - pun[money - i - 1] - 1);
      }
      int l = pun[money - (k - 2)] + 1;
      solve(slov + n - l, sirena, l, t);
      for (int i = 0; i < l; ++i) {
        slov[n - l + i] = t[i];
      }
      update(answ, slov, n);
    }
  }
  printf("%s\n", answ);
  return 0;
}