#include <climits>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define MAX 50
vector<string> vWord;
string targ = "";
bool vis[MAX + 1];
int ans = INT_MAX;
void DFS(string begin, int depth)
{
if (targ == begin)
{
ans = min(ans, depth);
return;
}
for (int i = 0; i < vWord.size(); i++)
{
int cnt = 0;
for (int j = 0; j < vWord[i].size(); j++)
{
if (begin[j] != vWord[i][j])
cnt++;
if (cnt == 2)
break;
}
if (cnt == 1)
{
// 백트래킹 수행
if (!vis[i])
{
vis[i] = true;
DFS(vWord[i], depth + 1);
vis[i] = false;
}
}
}
}
int solution(string begin, string target, vector<string> words)
{
vWord.assign(words.begin(), words.end());
targ = target;
DFS(begin, 0);
if(ans == INT_MAX)
ans=0;
return ans;
}