#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, std::less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int M = 1e9 + 7;
const int N = 2e5 + 10;
// 2nd aug Amazon ML Summer OA training
void printBinary(int num)
{
for (int i = 10; i >= 0; i--)
{
cout << (num >> i & 1);
}
cout << endl;
}
ll binaryitr(ll a, ll b)
{
ll ans = 1;
while (b)
{
if (b & 1)
{
ans = (ans * a) % M;
}
a = (a * a) % M;
b >>= 1;
}
return ans;
}
ll fact[N], Infact[N];
void factandInfact()
{
ll cnt = 1;
fact[0] = 1;
Infact[0] = 1;
for (int i = 1; i <= N; i++)
{
fact[i] = (fact[i - 1] * i) % M;
Infact[i] = binaryitr(fact[i], M - 2);
}
}
ll ncr(ll a, ll b)
{
if (b > a)
return 0;
return (fact[a] * (Infact[b]) % M * Infact[a - b] % M) % M;
}
class DisjointSet
{
vector<int> rank, par, Size;
public:
DisjointSet(int n)
{
rank.resize(n + 1, 0);
par.resize(n + 1);
Size.resize(n + 1, 1);
for (int i = 0; i <= n; i++)
{
par[i] = i;
}
}
int findPar(int node)
{
if (node == par[node])
return node;
return par[node] = findPar(par[node]);
}
void UnionByrank(int u, int v)
{
int ulp_u = findPar(u);
int ulp_v = findPar(v);
if (ulp_u == ulp_v)
return;
if (rank[ulp_u] > rank[ulp_v])
{
par[ulp_v] = ulp_u;
}
else if (rank[ulp_u] < rank[ulp_v])
{
par[ulp_u] = ulp_v;
}
else
{
par[ulp_v] = ulp_u;
rank[ulp_u]++;
}
}
void UnionBySize(int u, int v)
{
int ulp_u = findPar(u);
int ulp_v = findPar(v);
if (ulp_u == ulp_v)
return;
if (Size[ulp_u] > Size[ulp_v])
{
par[ulp_v] = ulp_u;
Size[ulp_u] += Size[ulp_v];
}
else
{
par[ulp_u] = ulp_v;
Size[ulp_v] += Size[ulp_u];
}
}
};
class Node
{
public:
Node *links[26];
bool flag = false;
bool containkey(char ch)
{
return links[ch - 'a'] != nullptr;
}
void put(char ch, Node *node)
{
links[ch - 'a'] = node;
}
Node *get(char ch)
{
return links[ch - 'a'];
}
void setEnd()
{
flag = true;
}
bool isEnd()
{
return flag;
}
};
class Trie
{
private:
Node *root;
public:
Trie()
{
root = new Node();
}
void insert(string word)
{
Node *node = root;
for (int i = 0; i < word.size(); i++)
{
if (!(node->containkey(word[i])))
{
node->put(word[i], new Node());
}
node = node->get(word[i]);
}
node->setEnd();
}
bool search(string word)
{
Node *node = root;
for (int i = 0; i < word.size(); i++)
{
if (!(node->containkey(word[i])))
{
return false;
}
node = node->get(word[i]);
}
return node->isEnd();
}
bool startsWith(string prefix)
{
Node *node = root;
for (int i = 0; i < prefix.size(); i++)
{
if (!(node->containkey(prefix[i])))
{
return false;
}
node = node->get(prefix[i]);
}
return true;
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
map<char, int> mp;
stack<char> st;
string ans;
for (auto ch : s)
{
mp[ch]++;
}
for (auto ch : s)
{
st.push(ch);
mp[ch]--;
if(mp[ch]==0) mp.erase(ch);
while (!st.empty() && mp.size() > 0 && st.top() <= (mp.begin()->first))
{
char key = st.top();
st.pop();
ans.push_back(key);
}
}
while (!st.empty())
{
ans.push_back(st.top());
st.pop();
}
cout << ans << endl;
return 0;
}