using System;
using System.Collections.Generic;
class GFG
{
static int MinLength(string a, string b)
{
Dictionary<char, int> Char
= new Dictionary<char, int>();
for (int i = 0; i < b.Length; i++) {
if (Char.ContainsKey(b[i])) {
Char[b[i]] += 1;
}
else {
Char[b[i]] = 1;
}
}
Dictionary<char, List<int> > CharacterIndex
= new Dictionary<char, List<int> >();
for (int i = 0; i < a.Length; i++) {
char x = a[i];
if (Char.ContainsKey(x)) {
if (!CharacterIndex.ContainsKey(x)) {
CharacterIndex[x] = new List<int>();
}
CharacterIndex[x].Add(i);
}
}
int len = int.MaxValue;
bool flag;
while (true) {
flag = true;
int firstVar = 0, lastVar = 0;
for (int i = 0; i < b.Length; i++)
{
if (i == 0)
{
if (!CharacterIndex.ContainsKey(b[i])) {
flag = false;
break;
}
else {
int x = CharacterIndex[b[i]][0];
CharacterIndex[b[i]].RemoveAt(0);
firstVar = x;
lastVar = x;
}
}
else {
bool elementFound = false;
foreach(int e in CharacterIndex[b[i]])
{
if (e > lastVar)
{
elementFound = true;
lastVar = e;
break;
}
}
if (elementFound == false) {
flag = false;
break;
}
}
}
if (flag == false) {
break;
}
len = Math.Min(len, Math.Abs(lastVar - firstVar)
+ 1);
}
return len;
}
static void Main(string[] args)
{
string a = "abcdefababaef";
string b = "abf";
int len = MinLength(a, b);
if (len != int.MaxValue) {
Console.WriteLine(len);
}
else {
Console.WriteLine("Impossible");
}
}
}